oatpp / oatpp-websocket

oatpp-websocket submodule.

Home Page:https://oatpp.io/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

API question

Aymsd opened this issue · comments

commented

In file: oatpp-websocket/Handshaker.hpp

Question

First of all I want to thank you for this amazing framework.
I have a small question regarding a use case of oat++ websocket.
in my case I'm trying to set up a websocket (with stomp) I have almost made it work but I'm still missing a header while doing the handshake.
The header I'm missing in this case is "Sec-WebSocket-Protocol" this one is expected from the client's side.
My issue is that I cannot add a header using the putHeader function. as soon as I do that the Handshake does not happen anymore.
Here is a sample of my controller. I'm guessing that I should not do it in an Action act(). But what would be the solution then please ?

`

ENDPOINT_ASYNC("GET", "PPLATFORM", WS){

    ENDPOINT_ASYNC_INIT(WS)

        Action act() override{
auto response = oatpp::websocket::Handshaker::serversideHandshake(request->getHeaders(), controller->websocketConnectionHandler);
response->putHeader("Sec-WebSocket-Protocol", "v11.stomp");   /* This is my issue */

return _return(response);
}

}

`

Thank you so much in advance

Hello @Aymsd ,

It looks like you are doing everything right.

You can find the handshake with subprotocol example in this issue - #31

Here is a sample of my controller. I'm guessing that I should not do it in an Action act(). But what would be the solution then please ?

It is safe to call Handshaker::serversideHandshake in act() method. Nothing wrong here.

My issue is that I cannot add a header using the putHeader function. as soon as I do that the Handshake does not happen anymore.

What do you mean by "does not happen anymore." do you receive any errors?

commented

Thank you for the reply.

Yes that issue does help. I cannot however put 5 arguments to the ENDPOINT_ASYNC macro. (I tried to copy it but I get: error: macro "ENDPOINT_ASYNC" passed 5 arguments, but takes just 3) (this might be just because i'm doing something stupid as I usually do..)

But when I said the handshake does not happen anymore what I tried to say is that the connection fails without even getting a handshake from my app.

To be more specific mainly my issue is that :
I use two clients to test the app:
1- Client that does not verify sub-protocol : this one does work just fine if I do not add the "response->putHeader("Sec-WebSocket-Protocol", "v11.stomp");"
As soon as I add that it fails "WebSocket connection to 'ws://127.0.0.1:7499/PPLATFORM' failed" with no more detail from my debug tool

2- Client that does check for the subprotocol and this one raises the issue:
"WebSocket connection to 'ws://[127.0.0.1:7499/PPLATFORM](http://127.0.0.1:7499/PPLATFORM)' failed: Error during WebSocket handshake: Sent non-empty 'Sec-WebSocket-Protocol' header but no response was received "
if do not add the "response->putHeader("Sec-WebSocket-Protocol", "v11.stomp");".

When I do add it I get an issue WebSocket connection to ws://127.0.0.1:7499/PPLATFORM failed: Connection closed before receiving a handshake response

Thank you so much again.

Hello @Aymsd ,

It looks like you are doing everything right.
Make sure that the name of the subprotocol matches - the one requested by the client and the one you include in response from the server.

Basically, your client should include Sec-WebSocket-Protocol: v11.stomp header, and your server should return a successful handshake response with the exact same header.

Yes that issue does help. I cannot however put 5 arguments to the ENDPOINT_ASYNC macro. (I tried to copy it but I get: error: macro "ENDPOINT_ASYNC" passed 5 arguments, but takes just 3) (this might be just because i'm doing something stupid as I usually do..)

ENDPOINT_ASYNC doesn't support header mapping like simple API does. Instead you have to use direct calls to request->getHeader()

Client that does check for the subprotocol and this one raises the issue:
"WebSocket connection to 'ws://127.0.0.1:7499/PPLATFORM' failed: Error during WebSocket handshake: Sent non-empty 'Sec-WebSocket-Protocol' header but no response was received "
if do not add the "response->putHeader("Sec-WebSocket-Protocol", "v11.stomp");".

When I do add it I get an issue WebSocket connection to ws://127.0.0.1:7499/PPLATFORM failed: Connection closed before receiving a handshake response

This looks correct. Please double-check the following:

  • The name of the subprotocol sent by the client matches the name of the subprotocol you are answering to the client from the server.
  • Try to identify why the connection is closed? It may be that the client receives a different subprotocl name and closes the connection.
commented

Thank you for the reply.

That makes sens.
I will try maybe using some other websocket debug tools.
I will update this in the next few days.

Thank you so much.