The ThreadServer can be used to easily create a multithreaded server where each thread polls multiple connections. To use it, at a minimum you must override or rebind clientConnected, readClientMessage, and clientMessage and you must define your Client and Message.


new ()

Creates a ThreadServer.



Polling timeout.


Stream to send error messages.


Space allocated to buffers when they are created.


Number of total connections the server will accept.


Maximum size of buffered data read from a socket. An exception is thrown if the buffer exceeds this value.


The most sockets a thread will handle.


Minimum message size.


Number of server threads.


Time between calls to update.


addSocket (s:Socket):Void

Called when the server gets a new connection.

dynamicafterEvent ():Void

Called after a client connects, disconnects, a message is received, or an update is performed.

dynamicclientConnected (s:Socket):Client

Called when a client connects. Returns a client object.

dynamicclientDisconnected (c:Client):Void

Called when a client disconnects or an error forces the connection to close.

dynamicclientMessage (c:Client, msg:Message):Void

Called when a message has been received. Message handling code should go here.

dynamiconError (e:Dynamic, stack:Array<StackItem>):Void

Called when an error has occurred.

dynamicreadClientMessage (c:Client, buf:Bytes, pos:Int, len:Int):{msg:Message, bytes:Int}

Called when data has been read from a socket. This method should try to extract a message from the buffer. The available data resides in buf, starts at pos, and is len bytes wide. Return the new message and the number of bytes read from the buffer. If no message could be read, return null.

run (host:String, port:Int):Void

Start the server at the specified host and port.

sendData (s:Socket, data:String):Void

Sends data to a client.

stopClient (s:Socket):Void

Shutdown a client's connection and remove them from the server.

dynamicupdate ():Void

This method is called periodically. It can be used to do server maintenance.

work (f:Void ‑> Void):Void

Internally used to delegate something to the worker thread.