RejectedSoftware Forums

Sign up

noob problem with WebSocket and Task

Hi, I'm trying to create an application server that use WebSocket and Redis.

I've started from the example vibenotes to be able to use the broadcast feature.
I also need non broadcast messages but I can't make it work.

I can send message with: socket.send("foo");
But everytimes I'm trying to get an information from Redis (in an other function) the socket is broken when I want to use it.

void callback (scope WebSocket socket) {
	auto sockid = cast(void*)socket;

	auto sendtask = runTask({
		while(socket.connected) {
			auto message = receiveOnly!string();
			socket.send(message);
		}
	});

	m_sockets[sockid] = SocketInfo(sendtask, channel);
	scope (exit) m_sockets.remove(sockid);

	while (socket.waitForData()) {
		auto data = socket.receiveText();
		auto msg = parseJsonString(data);
		sendtask.send("Message sent");
		msg = functionThatDoSomethingWithREDIS(msg);
		sendtask.send(msg.toString);
		// ERROR:
		// WebSocket handler failed: Got JSON of type undefined, expected object.
		// Task terminated with uncaught exception: WebSocket connection already actively closed.
	}
}

I know that the connection with Redis use Fiber to don't wait for the response and let the CPU do something else, but I don't understand how to use it correctly.

Also, I got the same error If I try to pass the sendTask to the functionThatDoSomethingWithREDIS: I can't call send without error.

Re: noob problem with WebSocket and Task

Maybe, it's an error about the JSON format... :|