Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // stop the playhead, server doesnt need it.
- stop();
- //===================================================
- // IMPORT ALL DEPENDENCIES
- import flash.events.Event;
- import flash.events.ServerSocketConnectEvent;
- import flash.net.ServerSocket;
- import flash.events.InvokeEvent;
- import flash.desktop.NativeApplication;
- import flash.data.SQLConnection;
- import flash.filesystem.File;
- //===================================================
- // ESTABLISH VARIABLES
- var $sIP:String = "127.0.0.1";
- var $sPort:Number = 8087;
- // ESTABLISH CONSTANTS
- var $sSocket:ServerSocket; // core server socket, listens for connections etc eventually.
- var $sChar = String.fromCharCode(1); // character that seperates data in parses
- var $eChar = String.fromCharCode(0); // character that ends data parses
- var $cArray = new Array();
- //===================================================
- // GENERAL FUNCTIONS
- function printToServerDialogBox(s:String):void // prints messages into server textbox
- {
- trace_txt.appendText("[ " + s + " ]"); // appends new message to existing contents of the box
- }
- function cCloseConnection( event:Event ):void // runs when a client breaks a connection
- {
- printToServerDialogBox("Client closed their connection.");
- }
- function fireUpTheSockets():void // starts the server listening for connections
- {
- $sSocket = new ServerSocket(); // new instance of serversocket class
- $sSocket.addEventListener( Event.CONNECT, cOpenConnection ); // hook a function to handle client connects
- //$sSocket.addEventListener( Event.CLOSE, sServerShutDown ); // hook a function to handle server shutdown?
- $sSocket.bind( $sPort, $sIP ); // configure what port the server is going to listen on? why the need for IP? strange.
- $sSocket.listen(); // start listening for connections
- printToServerDialogBox("Listening on " + $sSocket.localPort); // trace startup progress
- }
- function runServer():void
- {
- //fireUpTheDatabase // connects to the sqlite database
- fireUpTheSockets(); // starts the server listening for connections
- }
- //===================================================
- // RUN THE SERVER
- runServer();
- //===================================================
- function cOpenConnection(e:ServerSocketConnectEvent):void
- {
- printToServerDialogBox("Client opened a connection.");
- var c:Number = 0;
- for (c=0; c<=5000; c++)
- {
- if ($cArray[c] == undefined)
- {
- printToServerDialogBox("Client assigned index " + c);
- $cArray[c] = new Array();
- $cArray[c][0] = e.socket;
- $cArray[c][0].addEventListener(ProgressEvent.SOCKET_DATA, cDataRecieved);
- $cArray[c][0].addEventListener(Event.CLOSE, cCloseConnection);
- c = 5001;
- }
- }
- }
- function cDataRecieved(e: ProgressEvent):void
- {
- // SOMEHOW I NEED THE VALUE OF C FROM ABOVE INTO HERE
- var s:String = e.target.readUTFBytes(e.target.bytesAvailable);
- printToServerDialogBox("Client " + index + "sent packet " + s);
- if (s != ""){
- var myParse:Array = s.split($sChar);
- myParse[0].toString();
- var PACKETHEADER:String = myParse[0].toUpperCase()
- printToServerDialogBox("Packet header was stripped as" + PACKETHEADER);
- }
- }
- class MyStuff
- {
- // TODO: if you need the "parent" (whatever that maybe :), add the attribute here too
- var socket : flash.net.Socket;
- var c : Int;
- public function new(socket : flash.net.Socket, c : Int)
- {
- this.c = c;
- this.socket = socket;
- this.socket.addEventListener(ProgressEvent.SOCKET_DATA, cDataRecieved);
- // you need to add the method cCloseConnection
- this.socket.addEventListener(Event.CLOSE, cCloseConnection);
- }
- function cDataRecieved(e: ProgressEvent):void
- {
- // SOMEHOW I NEED THE VALUE OF C FROM ABOVE INTO HERE
- // access your c as:
- var tmp = this.c;
- var s:String = e.target.readUTFBytes(e.target.bytesAvailable);
- printToServerDialogBox("Client " + index + "sent packet " + s);
- if (s != ""){
- var myParse:Array = s.split($sChar);
- myParse[0].toString();
- var PACKETHEADER:String = myParse[0].toUpperCase()
- printToServerDialogBox("Packet header was stripped as" + PACKETHEADER);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement