Am 10/17/2012 11:34 PM, schrieb Eldar Insafutdinov:

Opening of the previous thread throws an exception in the vibenews
server, so I'll start a new one.

For some reason std.base64 does not like the base64 that my Thunderbird
sent... I've updated VibeNews to hadle this gracefully now, but still
need to investigate the exact cause.

After playing with websockets, I wrote something very simple that
resembles Socket.IO API:

   auto io = new IoSocketManager;

   auto router = new UrlRouter;
   router.get("/socketio", &io.handleRequest); // endpoint for connection

   // connection handle
   io.onConnection( (socket) {
       // message handler, data is Json
       socket.onMessage( (data) {
           // send message
           socket.send(Json(["message" : Json("success")]));
           // broadcast message to all sockets except this one
           socket.broadcast(data);
       });
   });

And Javascript:

 var socket = IoSocket.connect("/socketio");

 socket.on("connect", function() {
   socket.send({"message": "hello"});
 });

 socket.on("message", function(data) {
   console.log(data.message);
 });

All messages are encoded in Json for convenience. There's more to be
added: reconnect on connection lost, connection timeouts, heartbeats(do
you relly need them with Websockets?). I actually thought of
implementing Socket.IO protocol, but that seemed too heavyweight and
geared towards supporting multiple transports, so I decided to
concentrate on Websockets for now. The code is at
github. There is a test app as well.

Supporting the Socket.IO protocol was our original plan for the
vibe.http.websocket module - to be able to reuse the Socket.IO code on
the JavaScript side, and easy porting of projects that use SIO.

Mirroring the JS API may be a good alternative, though. My hope also was
that it would be possible to concentrate on WebSockets and leave out the
fallbacks, but IE < 10 will probably still be sticking around for quite
a while...

As to heartbeats, I think they are supposed to be handled on the
protocol layer. But last time we checked, the browser implementations
were still quite far from the non-existent standard and more or less
only really supported plain sending of data messages.