WebSocket handshake not working when using Sec-WebSocket-Protocol header
See original GitHub issueHi,
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:
- Created 3 years ago
- Comments:5 (3 by maintainers)
Top GitHub Comments
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. 😃
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.