RejectedSoftware Forums

Sign up

most simple chat using websocket?

import vibe.d;

static this()
{
  WebSocket[] client_list;

  auto router = new UrlRouter;
  router.get("/", handleWebSockets(delegate(WebSocket sock){
    client_list ~= sock;
    while( sock.connected ){
      auto msg = sock.receive();
      foreach(client; client_list) client.send(msg); //=>"ERROR: Operating on TcpConnection owned by a different fiber!"
    }
  }));

  auto settings = new HttpServerSettings;
  settings.port = 9998;
  listenHttp(settings, router);
}

I found https://github.com/rejectedsoftware/vibe.d/issues/50
I want to call acquire(), but WebSocket class don't have acquire().
How do you think most simple websocket chat?

Re: most simple chat using websocket?

On Mon, 22 Oct 2012 17:31:53 GMT, kyubuns wrote:

import vibe.d;

static this()
{
  WebSocket[] client_list;

  auto router = new UrlRouter;
  router.get("/", handleWebSockets(delegate(WebSocket sock){
    client_list ~= sock;
    while( sock.connected ){
      auto msg = sock.receive();
      foreach(client; client_list) client.send(msg); //=>"ERROR: Operating on TcpConnection owned by a different fiber!"
    }
  }));

  auto settings = new HttpServerSettings;
  settings.port = 9998;
  listenHttp(settings, router);
}

I found https://github.com/rejectedsoftware/vibe.d/issues/50
I want to call acquire(), but WebSocket class don't have acquire().
How do you think most simple websocket chat?

Using acquire()/release() should make it work (will be fixed) - although the resulting code could then still throw, because multiple messages might arrive in parallel and one fiber could try to acquire a WebSocket that is already owned by a different fiber. A vibe.core.mutex.Mutex would also be needed to make if safe.

A simple queue+signal based implementation is at https://github.com/rejectedsoftware/vibenotes/blob/master/source/vibenotes/broadcast.d (*). Putting such a class into vibe.http.websocket is also on the agenda, because it should be a quite common need and it contains some pretty low level code (rawYield()).

(*) the latest master is needed for this to work because it needs a fix that Eldar Insafutdinov recently committed.

Re: most simple chat using websocket?

On Mon, 22 Oct 2012 17:31:53 GMT, kyubuns wrote:

import vibe.d;

static this()
{
  WebSocket[] client_list;

  auto router = new UrlRouter;
  router.get("/", handleWebSockets(delegate(WebSocket sock){
    client_list ~= sock;
    while( sock.connected ){
      auto msg = sock.receive();
      foreach(client; client_list) client.send(msg); //=>"ERROR: Operating on TcpConnection owned by a different fiber!"
    }
  }));

  auto settings = new HttpServerSettings;
  settings.port = 9998;
  listenHttp(settings, router);
}

I found https://github.com/rejectedsoftware/vibe.d/issues/50
I want to call acquire(), but WebSocket class don't have acquire().
How do you think most simple websocket chat?

If you are prepared to deal with the alpha quality code I'm working on a Socket.IO implementation for vibe.d https://github.com/eldar/socket.io-d/

A simple example is included with the source code (app.d for server and socket.js for client) It supports the callback model of reference implementation for Node, which is somewhat simpler than writing a while() loop. So for a simple chat you will have to listen for the event on the socket and then broadcast it to all the clients using broadcast_emit() function. It's not documented yet so let me know if there are any problems(and there will be many!). I'm currently working on transports other than Websockets, in particular xhr-polling.

Re: most simple chat using websocket?

Am 10/23/2012 1:19 AM, schrieb Eldar Insafutdinov:

If you are prepared to deal with the alpha quality code I'm working on a
Socket.IO implementation for vibe.d
https://github.com/eldar/socket.io-d/

Btw. you could fill out the package.json and register the github
repository at http://registry.vibed.org. It would then already be
usable as a dependency using e.g. "socket.io-d" : "~master" even if it
does not have a (tagged) version yet.

Re: most simple chat using websocket?

On Tue, 23 Oct 2012 09:39:11 +0200, Sönke Ludwig wrote:

Am 10/23/2012 1:19 AM, schrieb Eldar Insafutdinov:

If you are prepared to deal with the alpha quality code I'm working on a
Socket.IO implementation for vibe.d
https://github.com/eldar/socket.io-d/

Btw. you could fill out the package.json and register the github
repository at http://registry.vibed.org. It would then already be
usable as a dependency using e.g. "socket.io-d" : "~master" even if it
does not have a (tagged) version yet.

Good, just added, although I confused myself and added it twice and now I can't delete the previous one called "my-project". Maybe because it takes 30 minutes to be reflected in the package list, so I'll wait.

Re: most simple chat using websocket?

Am 10/24/2012 12:33 AM, schrieb Eldar Insafutdinov:

On Tue, 23 Oct 2012 09:39:11 +0200, Sönke Ludwig wrote:

Am 10/23/2012 1:19 AM, schrieb Eldar Insafutdinov:

If you are prepared to deal with the alpha quality code I'm working on a
Socket.IO implementation for vibe.d
https://github.com/eldar/socket.io-d/

Btw. you could fill out the package.json and register the github
repository at http://registry.vibed.org. It would then already be
usable as a dependency using e.g. "socket.io-d" : "~master" even if it
does not have a (tagged) version yet.

Good, just added, although I confused myself and added it twice and now
I can't delete the previous one called "my-project". Maybe because it
takes 30 minutes to be reflected in the package list, so I'll wait.

No sorry, the functionality was still missing in the back end. Should
work now.