:thread: Okhttp not adding WebSocket headers like ...
# squarelibraries
r
đź§µ Okhttp not adding WebSocket headers like automatically. Is this expected?
Copy code
"Connection" to "Upgrade",
            "Sec-WebSocket-Extensions" to "permessage-deflate",
            "Upgrade" to "websocket",
            "Sec-WebSocket-Version" to "13",
            "Sec-WebSocket-Key" to secWebSocketKeyValue
As per this RFC doc this is the part of handshake, So I think library should add these headers automatically right?
r
When I see it on ProxyMan or charles they are not added, they are missing
j
And you're actually making a web socket request and not a normal one?
r
Yes its websocket
But I figured out, Other piece of code where I had headers centralized it was replacing the headers instead of adding headers
Copy code
requestBuilder.headers(httpHeadersProvider.get().toHeaders())
I did not know
headers()
will replace all headers. Then I thought it does not have the verb so though to check this and it was replacing all headers including websocket ones Now its working when I am adding headers in a loop
I still have one more question why do I get
EOFException
when I manually call
webSocket.disconnect()
should not Okhttp invoke
onClosed
callback instead of
onFailure
?
j
What is this disconnect API you speak of?
OkHttp’s WebSocket has a graceful close (close) and an immediate one (cancel)
r
I call this API
Copy code
webSocket?.close(DISCONNECT_CODE, DISCONNECT_REASON)
// Disconnect codes
 DISCONNECT_CODE = 1000
 DISCONNECT_REASON = "Normal closure"
I expected that I will get a callback in
onClosed
but I get the callback in
onFailure
with
EODException
I could see
webSocket.close()
returns true but somehow I get the callback in
onFailure
with this I am on 4.9.3 okhttp version
Copy code
Exception " java.io.EOFException 
                                                                                                                           stacktrace: java.io.EOFException
                                                                                                    at okio.RealBufferedSource.require(RealBufferedSource.kt:202)
                                                                                                    at okio.RealBufferedSource.readByte(RealBufferedSource.kt:212)
                                                                                                    at okhttp3.internal.ws.WebSocketReader.readHeader(WebSocketReader.kt:119)
                                                                                                    at okhttp3.internal.ws.WebSocketReader.processNextFrame(WebSocketReader.kt:102)
                                                                                                    at okhttp3.internal.ws.RealWebSocket.loopReader(RealWebSocket.kt:293)
                                                                                                    at okhttp3.internal.ws.RealWebSocket$connect$1.onResponse(RealWebSocket.kt:195)
                                                                                                    at okhttp3.internal.connection.RealCall$AsyncCall.run(RealCall.kt:519)
In the real websocket class, I see this where the code fails
j
Does your server implement graceful close?
r
I am not sure about this. What do I need to check in order to confirm whether the server implement graceful close?
j
If it did I think you’d get the callback you want
👍 1