RejectedSoftware Forums

Sign up

Websockets(restored)

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

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.

Re: Websockets(restored)

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.

Re: Websockets(restored)

Am 10/18/2012 8:45 AM, schrieb Sönke Ludwig:

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.

Okay it was Base64.Decoder not accepting line breaks in the input (which
doesn't really make sense, as they are obligatory most of the time).
They are now filtered out beforehand.

Re: Websockets(restored)

On Thu, 18 Oct 2012 08:45:55 +0200, Sönke Ludwig wrote:

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.

I completely missed out that websockets are not supported in IE9. Anyway, I am now trying to implement Socket.IO protocol, with Websockets to begin with. For some strange reason client gets disconnected even though it receives regular heartbeats. I'm investigating it.