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.