Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

API question #33

Open
Aymsd opened this issue Feb 8, 2022 · 4 comments
Open

API question #33

Aymsd opened this issue Feb 8, 2022 · 4 comments

Comments

@Aymsd
Copy link

Aymsd commented Feb 8, 2022

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

@lganzzzo
Copy link
Member

lganzzzo commented Feb 8, 2022

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?

@Aymsd
Copy link
Author

Aymsd commented Feb 8, 2022

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.

@lganzzzo
Copy link
Member

lganzzzo commented Feb 9, 2022

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.

@Aymsd
Copy link
Author

Aymsd commented Feb 10, 2022

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants