The code for this post is here.
In a previous post I explained how to create a socket connection from javascript to a server using Haxe. Unfortunately, this technique requried using flash as a proxy between javascript and the server. As I am working on a game for Mozilla’s game on competition I needed to find a flash free way. This is where websockets comes in. Websockets are a new standard way to open a continuous connection with a server. There are many articles explaining this technology and so I won’t try to. This article is about my adventure with setting up a WebSocket server using Haxe.
If you want to skip my ramblings, and are just after a working code example of building a threaded websocket server in Haxe: see here for the code, and here for the working demo (though it may not be running anymore.)
Rambling on…
Googling ‘websocket’ and ‘haxe’ doesn’t result in much. Except for the hxWebSockets library. Credits go to the author of this code, however, upon downloading and running it I encountered a couple of issues. Firstly, it doesn’t work. Secondly, it is not obvious how to use it. Thirdly, you typically want a threaded server, and it wasn’t obvious how to go about using this code to do that.
After figuring out how to set up and use hxWebSockets I was wondering why it was not working. After reading streams of bytes and http headers, I eventually figured out that it was because the WebSocket protocol had changed! This is to be expected as it is a draft standard, and probably by the time you are reading this article, it might have changed again, rendering my code and demo obsolete. The best explanation of the new server-side protocol I could find is here, and so I went about integrating the new components into the hxWebSockets code.
You can find the code for the updated WebSocket.hx (click files). The file EchoServer.hx demonstrates how to code a basic websocket server using WebSocket.hx.
The next step was to look at building a websocket server that could handle multiple connections simultaneously. Luckily the ThreadServer that comes with the haxe standard library is exactly what I needed. Unfortunately WebSocket.hx is built in a way that made it tricky to incorporate into the threadserver model, so I pulled it apart, resulting in WSThreadedServer.hx, which is a ThreadServer class that implements the websocket protocol. WSThreadedServer parses the http style headers that a websocket passes in and sends back a draft 76 handshake to the client. I have uploaded a working example which demonstrates WSThreadedServer in action. For all the people viewing that page, the cursor movements in each browser are broadcast to all other browsers (if you can’t see any boxes moving around, try opening multiple browsers.) Of course the demo requires a WebSockets compatible browser like Chrome or Safari in order to work. The file testpointers.html contains the client-side logic. Its in pure JS at the moment, though coding it in HX would be trivial.
I hope my code is useful to other haxe developers looking for websocket joy!