oatpp / oatpp-websocket

oatpp-websocket submodule.
https://oatpp.io/
Apache License 2.0
78 stars 32 forks source link

How to apply subprotocol parameter #31

Open abitcyanine opened 2 years ago

abitcyanine commented 2 years ago

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

lganzzzo commented 2 years ago

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

abitcyanine commented 2 years ago

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