Advertisement
Guest User

Untitled

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