oatpp-websocket icon indicating copy to clipboard operation
oatpp-websocket copied to clipboard

How to apply subprotocol parameter

Open abitcyanine opened this issue 2 years ago • 2 comments

Our old server applies the subprotocol parameter on websocket. Now we need to use oatpp in the server. How to apply the subprotocol parameter?

abitcyanine avatar Dec 10 '21 11:12 abitcyanine

Hello @abitcyanine ,

oatpp doesn't check for subprotocol and at the moment just ignores it. So you have to manually add a subprotocol check in the handshake. Should be something like that:

ENDPOINT("GET", "ws", ws, 
         HEADER(String, subprotocol, "Sec-WebSocket-Protocol"), //< protocol header
         REQUEST(std::shared_ptr<IncomingRequest>, request)) 
{
  /* check subprotocol value */
  OATPP_ASSERT_HTTP(subprotocol == "my-subprotocol", Status::CODE_400, "unknown subprotocol")
  
  /* do the handshake */
  auto response = oatpp::websocket::Handshaker::serversideHandshake(request->getHeaders(), websocketConnectionHandler);

  /* agree on subprotocol */
  response->putHeader("Sec-WebSocket-Protocol", "my-subprotocol");
  
  return response;
};

Regards, Leonid

lganzzzo avatar Dec 10 '21 15:12 lganzzzo

OK, I'll try the method you provided. Thank you for your reply

abitcyanine avatar Dec 19 '21 08:12 abitcyanine