RejectedSoftware Forums

Sign up

Support for WebSocket sub-protocols ?

Hi,

Is there a support for WebSocket sub-protocols? I can't find any reference for it.
As I understand, the client will send a separate 'Sec-WebSocket-Protocol:abc,def,xyz' header, and the server should respond with an appropriate response header, specifying the preferred protocol.
Unfortunately, inside the WebSocket handler delegate, I can't manipulate the http response headers - I guess, it's already sent.

Regards,
Zsombor

Re: Support for WebSocket sub-protocols ?

On Sun, 25 Oct 2015 09:44:12 GMT, Zsombor wrote:

Hi,

Is there a support for WebSocket sub-protocols? I can't find any reference for it.
As I understand, the client will send a separate 'Sec-WebSocket-Protocol:abc,def,xyz' header, and the server should respond with an appropriate response header, specifying the preferred protocol.
Unfortunately, inside the WebSocket handler delegate, I can't manipulate the http response headers - I guess, it's already sent.

Regards,
Zsombor

Right, the WebSocket callback happens too late. But you could do this by manually writing out the HTTP request handler:

void webSocketRequestHandler(scope HTTPServerRequest req, scope HTTPServerResponse res)
{
   void handler(scope WebSocket socket) {
      ...
   }

   if (auto protos = "Sec-WebSocket-Protocol" in req.headers) {
      ...
      res.headers["Sec-WebSocket-Protocol"] = ...;
   }

   handleWebSocket(&handler, req, res);
}