ServerSocketX

From Noisebridge
Revision as of 15:04, 7 April 2013 by Thex (Talk | contribs)

Jump to: navigation, search

Implementation of a basic "server" socket in AS3[1], default is a HTTP 0.9[2] web server implementation on port 8080. A running instance of this server generates a HTML HelloWorld page at http://localhost:8080

Request from client:

GET / HTTP/1.1

Response from Server:

<!DOCTYPE html>
<html>
<head>
<title>Hello HTML</title>
</head>
<body>
<p>Hello World!</p>
</body>
</html>

Source Code:

package
{
	import flash.display.Sprite;
	import flash.net.ServerSocket;
	import flash.events.ServerSocketConnectEvent;
	import flash.net.Socket;
	import flash.events.ProgressEvent;
	import flash.events.IOErrorEvent;
	import flash.events.Event;
	import flash.events.OutputProgressEvent;
	
	public class ServerSocketX extends Sprite
	{
		private var serverSocket:ServerSocket;
		private var clientSockets:Object;

		public function ServerSocketX() 
		{
			clientSockets = [];
			
			serverSocket = new ServerSocket();
			
			serverSocket.addEventListener(Event.CONNECT, connectHandler);
			serverSocket.addEventListener(Event.CLOSE, onClose);
			
			serverSocket.bind(8080, "127.0.0.1");
			
			serverSocket.listen();
			
			trace("Server Socket listenting.");
		}
		
		private function connectHandler(e:ServerSocketConnectEvent):void
		{
			var socket:Socket = e.socket as Socket;
			clientSockets[socket.remoteAddress + socket.remotePort] = socket;
			
			socket.addEventListener(ProgressEvent.SOCKET_DATA, socketDataHandler);
			socket.addEventListener(Event.CLOSE, onClientClose);
			socket.addEventListener(IOErrorEvent.IO_ERROR, onIOError);
			socket.addEventListener(OutputProgressEvent.OUTPUT_PROGRESS, progressHandler);
			
			trace("Connect: " + socket.remoteAddress + ":" + socket.remotePort);
		}
		
		private function closeSocket(socket:Socket):void
		{
			trace("Close: " + socket.remoteAddress + ":" + socket.remotePort);
			
			socket.removeEventListener(ProgressEvent.SOCKET_DATA, socketDataHandler);
			socket.removeEventListener(Event.CLOSE, onClientClose);
			socket.removeEventListener(IOErrorEvent.IO_ERROR, onIOError);
			socket.removeEventListener(OutputProgressEvent.OUTPUT_PROGRESS, progressHandler);
			
			clientSockets[socket.remoteAddress + socket.remotePort] = null;
			
			if (socket.connected)
				socket.close();
			
			socket = null;
		}
		
		private function progressHandler(e:OutputProgressEvent):void
		{
			if (e.bytesPending == 0)
			{
				var socket:Socket = e.target as Socket;
				closeSocket(socket);
			}
		}
		
		private function socketDataHandler(e:ProgressEvent):void
		{
			var socket:Socket = e.target as Socket;
			var msg:String = socket.readUTFBytes(socket.bytesAvailable);
			
			trace(socket.remoteAddress + ":" + socket.remotePort + "<" + msg.substr(0, msg.indexOf("\n")));
			
			// check for http GET /
			if (msg.substr(0, 6) == "GET / ")
			{
				socket.writeUTFBytes("<!DOCTYPE html><html><head><title>Hello HTML</title></head><body><p>Hello World!</p></body></html>");
				socket.flush();
			}
			else
			{
				closeSocket(socket);
			}
		}
		
		private function onClientClose(e:Event):void
		{
			var socket:Socket = e.target as Socket;
			closeSocket(socket);
		}
		
		private function onIOError(e:IOErrorEvent):void
		{
			trace("ERROR: " + e.text);
		}
		
		private function onClose(e:Event):void
		{
			trace("Server socket closed.");
		}

	}
	
}
Personal tools