Advertisement
Guest User

Untitled

a guest
Jul 19th, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. // stop the playhead, server doesnt need it.
  3. stop();
  4.  
  5. //===================================================
  6.  
  7. // IMPORT ALL DEPENDENCIES
  8. import flash.events.Event;
  9. import flash.events.ServerSocketConnectEvent;
  10. import flash.net.ServerSocket;
  11. import flash.events.InvokeEvent;
  12. import flash.desktop.NativeApplication;
  13. import flash.data.SQLConnection;
  14. import flash.filesystem.File;
  15.  
  16. //===================================================
  17.  
  18. // ESTABLISH VARIABLES
  19. var $sIP:String = "127.0.0.1";
  20. var $sPort:Number = 8087;
  21.  
  22. // ESTABLISH CONSTANTS
  23. var $sSocket:ServerSocket;                          // core server socket, listens for connections etc eventually.
  24. var $sChar = String.fromCharCode(1);                // character that seperates data in parses
  25. var $eChar = String.fromCharCode(0);                // character that ends data parses
  26.  
  27. var $cArray = new Array();
  28.  
  29. //===================================================
  30.  
  31. // GENERAL FUNCTIONS
  32.  
  33. function printToServerDialogBox(s:String):void      // prints messages into server textbox
  34. {
  35.     trace_txt.appendText("[ " + s + " ]");          // appends new message to existing contents of the box
  36. }
  37.  
  38. function cCloseConnection( event:Event ):void       // runs when a client breaks a connection
  39. {
  40.     printToServerDialogBox("Client closed their connection.");
  41. }
  42.  
  43. function fireUpTheSockets():void                    // starts the server listening for connections
  44. {
  45.    
  46.     $sSocket = new ServerSocket();                                      // new instance of serversocket class
  47.  
  48.     $sSocket.addEventListener( Event.CONNECT, cOpenConnection );        // hook a function to handle client connects
  49.     //$sSocket.addEventListener( Event.CLOSE, sServerShutDown );        // hook a function to handle server shutdown?
  50.                
  51.     $sSocket.bind( $sPort, $sIP );                                      // configure what port the server is going to listen on? why the need for IP? strange.
  52.                
  53.     $sSocket.listen();                                                  // start listening for connections
  54.    
  55.     printToServerDialogBox("Listening on " + $sSocket.localPort);       // trace startup progress
  56.    
  57. }
  58.  
  59. function runServer():void
  60. {
  61.     //fireUpTheDatabase                                                 // connects to the sqlite database
  62.     fireUpTheSockets();                                                 // starts the server listening for connections
  63. }
  64.  
  65. //===================================================
  66.  
  67. // RUN THE SERVER
  68. runServer();
  69.  
  70. //===================================================
  71.        
  72. function cOpenConnection(e:ServerSocketConnectEvent):void
  73. {
  74.     printToServerDialogBox("Client opened a connection.");
  75.     var c:Number = 0;
  76.    
  77.     for (c=0; c<=5000; c++)
  78.     {
  79.         if ($cArray[c] == undefined)
  80.         {
  81.             printToServerDialogBox("Client assigned index " + c);
  82.            
  83.             $cArray[c] = new Array();
  84.             $cArray[c][0] = e.socket;
  85.             $cArray[c][0].addEventListener(ProgressEvent.SOCKET_DATA, cDataRecieved);
  86.             $cArray[c][0].addEventListener(Event.CLOSE, cCloseConnection);
  87.            
  88.             c = 5001;
  89.            
  90.         }
  91.     }
  92. }
  93.  
  94. function cDataRecieved(e: ProgressEvent):void
  95. {
  96.    
  97.     // SOMEHOW I NEED THE VALUE OF C FROM ABOVE INTO HERE
  98.  
  99.     var s:String = e.target.readUTFBytes(e.target.bytesAvailable);
  100.    
  101.     printToServerDialogBox("Client " + index + "sent packet " + s);
  102.    
  103.     if (s != ""){
  104.  
  105.         var myParse:Array = s.split($sChar);
  106.         myParse[0].toString();
  107.        
  108.         var PACKETHEADER:String = myParse[0].toUpperCase()
  109.         printToServerDialogBox("Packet header was stripped as" + PACKETHEADER);
  110.        
  111.     }
  112.    
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement