RejectedSoftware Forums

Sign up

Websocket module doesn't work?

This really simple websocket example doesn't work.

Server:

import std.stdio;
import vibe.d;

shared static this()
{
    auto router = new URLRouter();
    router.get("/", handleWebSockets((sock){
        "connected".writeln();
        sock.send("foo");
    }));

    auto settings = new HTTPServerSettings();
    settings.bindAddresses = ["127.0.0.1"];
    settings.port = 1918;

    listenHTTP(settings, router);
}

Client:

<!doctype html>
<script type="text/javascript">
new WebSocket("ws://127.0.0.1:1918/").onmessage = function(e){
    alert(e.data);
}
</script>

This server written in nodejs works well.

var Server = require('ws').Server;

var server = new Server({
	host : '127.0.0.1',
	port : 1918
});

server.on('connection', function(sock) {
    console.log("connected");
    sock.send("foo");
});

Something wrong with my D code?

Re: Websocket module doesn't work?

On Sun, 08 Sep 2013 12:15:05 GMT, Hisayuki Mima wrote:

This really simple websocket example doesn't work.

(...)

Something wrong with my D code?

The problem was that the sent message was never finalize()d (i.e. the data frame was never actually sent). It worked by accident until recently until the default do_flush = true argument was removed recently. I've pushed a commit that will automatically finalize messages from now on:

https://github.com/rejectedsoftware/vibe.d/commit/c9ded6c177f8901daf8b17fb8a96364907bf425c