RejectedSoftware Forums

Sign up

WebSocket disconnecting

Hello. I'm trying to determine when a client is disconnected from the WebSocket server. According to the "websocket" example, server will write "Client disconnected." after while loop , but it never does. Here is my handler function:

void handleConnection(scope WebSocket socket) {
		logInfo("Someone became online!");
		while (socket.connected) {
			auto msg = socket.receiveText();
			socket.send(msg);
		}
		logInfo("Someone went offline!");
	}

The output has only "Someone became online!" message. I've tried to call socket.close()/reload page/close tab on client.

Listening for HTTPS requests on 127.0.0.1:9996
Someone became online!
Someone became online!

Re: WebSocket disconnecting

My setup:
vibe-d 0.7.23
wss connection
the handler and start function are in one class (Server).

class Server {
	
	void handleConnection(scope WebSocket socket) {
		logInfo("Someone became online!");
		while (socket.connected) {
			auto msg = socket.receiveText();
			socket.send(msg);
		}
		logInfo("Someone went offline!");
	}
	
	void start() {
		auto router = new URLRouter;
		router.get("/live", handleWebSockets(&handleConnection));
		
		auto settings = new HTTPServerSettings;
		settings.port = 9996;
		settings.bindAddresses = ["127.0.0.1"];
		// ssl settings
		settings.sslContext = createSSLContext(SSLContextKind.server);
        settings.sslContext.useCertificateChainFile("ssl/server.crt");
	settings.sslContext.usePrivateKeyFile("ssl/server.key");

		
		listenHTTP(settings, router);
	}
	
}

Re: WebSocket disconnecting

I found the problem. Server won't close the connection if I sent a message.

If I add

if(socket.dataAvailableForRead) {
    auto msg = socket.receiveText();
    logInfo("Received: ", msg);
}

I get:

Task terminated with unhandled exception: Acquiring reader of already owned connection.
Error executing command run: Program exited with code -11

So I need something like in the broadcast example, according to http://forum.rejectedsoftware.com/groups/rejectedsoftware.vibed/thread/566/