Categories
blog

Haxe Websockets (Part 2)

Read this post for Part 1…

I have just updated the haxe websocket code significantly. I have made a websocket compatible ThreadServer and ThreadRemotingServer, and included some different examples of using these classes.

The websocket.ThreadServer class can be used to easily set up a server that sends and receives messages from connected clients. There are two examples “echo” and “cursors” that demonstrate how to use it. For example to setup a basic websocket echoing server:

class EchoServer
{    
 public static var server:ThreadServer;
 public static function main()
 {
  server = new ThreadServer();
  server.onMessage = function(c:Client, m:String) {
   server.sendMessage(c, m);
  };
  server.run("localhost", 5001);
 }    
}

I also tediously hacked away at neko.net.ThreadRemotingServer and made a version that supports the latest websocket protocol. Check out the “remoting” example in the code to see how to use it.

You can browse the code, download a zip, or clone it directly:

hg clone http://hg.bp.io/public/websockets websockets

I haven’t had much time to debug the code, but it all seems to work okay. I would appreciate any bug reports, just leave them as comments on this post.