question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

WebSocket handshake not working when using Sec-WebSocket-Protocol header

See original GitHub issue

Hi,

recently I started migrating a small service from ktor to http4k and experienced some handshake issues when using the http4k-client-websocket (3.251.0) module, despite sending the exact same request to start the session as in the previous implementation (verified in Wireshark).

The code used to create the non-blocking client:

WebsocketClient.nonBlocking(Uri.of("ws://192.168.1.24:8214"), listOf("Sec-WebSocket-Protocol" to "Lux_WS")) {
    // not relevant, after handshake
}

This led to the error message 1002 draft Draft_6455 extension: DefaultExtension max frame size: 2147483647 refuses handshake in the onError-handler. After some digging around I found out that the value passed in the Sec-WebSocket-Protocol needs to be included as a protocol when creating the Draft_6455 instance. I tried replacing Draft_6455() with Draft_6455(emptyList<IExtension>(), headers.filter { "Sec-WebSocket-Protocol".equals(it.first, true) }.map {Protocol(it.second)}) in WebsocketClient.kt:83 and while it is surely not the most elegant solution it does indeed allow the handshake to complete.

Is there a better way of achieving this that I’m missing or is this a rather obscure edge case?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
daviddentoncommented, Jul 7, 2020

I’ve exposed the Draft now so that people don’t have to implement the entire client again if they want to use a custom protocol. It will be released in 3.253.0 whenever that lands. 😃

Closing for now. 😃

1reaction
rschlegel-cccommented, Jul 5, 2020

Thanks for the quick response. You’re right, I messed up the default case with the workaround I posted. I tried the proposed change and as far as I can tell it fixes my problem in a way that is consistent with the Java-WebSocket documentation (https://github.com/TooTallNate/Java-WebSocket/wiki/Specifing-a-Sec-WebSocket-Protocol) while keeping the API simple for more standard cases.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Error during WebSocket handshake: Sent non-empty 'Sec ...
Your server answers the websocket connection request with an empty Sec-WebSocket-Protocol header, since it doesn't support the Chat-1 ...
Read more >
Fix error about non-empty Sec-WebSocket-Protocol header
Introduction. When you encounter this problem about WebSocket connection to 'wss://<address>/live' failed: Error during WebSocket handshake: ...
Read more >
Sent non-empty 'Sec-WebSocket-Protocol' header but no ...
The |Sec-WebSocket-Protocol| header field is used in the WebSocket opening handshake. It is sent from the client to the server and back
Read more >
25. WebSocket Support - Spring
During the handshake, the client and server can use the header Sec-WebSocket-Protocol to agree on a sub-protocol, i.e. a higher, application-level protocol ...
Read more >
Sec-WebSocket-Protocol (Subprotocol) header support
During the handshake, the client would ask the server what kind of subprotocols are supported by sending Sec-WebSocket-Protocol header.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found