OkHTTP Websocket: Unexpected end of steam on Connection

11,530

Solution 1

The chat websocket server disregards the request unless it has both:

  • A url param l, corresponding to a value sent back when the list of events is loaded.
  • A header Origin set to "http://stackexchange.com".

You just have to add those to your request:

Request wsRequest = new Request.Builder()
        .url(wsUrl+"?l=" + valueFromLoadingEvents)
        .addHeader("Origin", "http://stackexchange.com")
        .build();

Solution 2

Most probably there are 2 things, happening at the same time.
First, the url contains a port which is not commonly used AND secondly, you are using a VPN or proxy that does not support that port.
Personally, I had the same problem.
My server port was 45860 and I was using pSiphon anti-filter VPN.
In that condition my Postman reported "connection hang-up" only when server's relpy was an error with status codes bigger than 0. (it was fine when some text was returning from server with no error code)
Then I changed my web service port to 8080 on my server and, WOW, it worked! although psiphon vpn was connected.
Therefore, my suggestion is that if you can change the server port, so try it, or check if there is a proxy problem.

Share:
11,530
Anubian Noob
Author by

Anubian Noob

Let's chill in Room 15! Check out my GitHub: https://github.com/anubiann00b Contact me by email: skraman1999 at gmail.

Updated on June 13, 2022

Comments

  • Anubian Noob
    Anubian Noob almost 2 years

    I'm trying to connect to the Stack Exchange chat Websocket. The websocket is used to receive new events in the chat, such as new messages.

    Here's the code used to create the Websocket:

    String wsUrl = getWsUrl();
    Request wsRequest = new Request.Builder()
            .url(wsUrl)
            .build();
    WebSocketCall wsCall = WebSocketCall.create(httpClient, wsRequest);
    wsCall.enqueue(new ChatWebSocketListener());
    

    The websocket URL is in this form:

    wss://chat.sockets.stackexchange.com/events/16/4b3a8a1f68704b8db35ce9f0915c7c45
    

    The WebSocketListener receives a onFailure response only, with this exception:

    E/IOException﹕ unexpected end of stream on Connection{chat.sockets.stackexchange.com:443, proxy=DIRECT@ hostAddress=192.111.0.32 cipherSuite=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 protocol=http/1.1} (recycle count=0)
        java.io.IOException: unexpected end of stream on Connection{chat.sockets.stackexchange.com:443, proxy=DIRECT@ hostAddress=192.111.0.32 cipherSuite=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 protocol=http/1.1} (recycle count=0)
                at com.squareup.okhttp.internal.http.HttpConnection.readResponse(HttpConnection.java:211)
                at com.squareup.okhttp.internal.http.HttpTransport.readResponseHeaders(HttpTransport.java:80)
                at com.squareup.okhttp.internal.http.HttpEngine.readNetworkResponse(HttpEngine.java:917)
                at com.squareup.okhttp.internal.http.HttpEngine.readResponse(HttpEngine.java:757)
                at com.squareup.okhttp.Call.getResponse(Call.java:274)
                at com.squareup.okhttp.Call$ApplicationInterceptorChain.proceed(Call.java:230)
                at com.squareup.okhttp.Call.getResponseWithInterceptorChain(Call.java:201)
                at com.squareup.okhttp.Call.access$100(Call.java:36)
                at com.squareup.okhttp.Call$AsyncCall.execute(Call.java:164)
                at com.squareup.okhttp.internal.NamedRunnable.run(NamedRunnable.java:33)
                at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
                at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
                at java.lang.Thread.run(Thread.java:831)
         Caused by: java.io.EOFException: \n not found: size=0 content=...
                at okio.RealBufferedSource.readUtf8LineStrict(RealBufferedSource.java:200)
                at com.squareup.okhttp.internal.http.HttpConnection.readResponse(HttpConnection.java:191)
                at com.squareup.okhttp.internal.http.HttpTransport.readResponseHeaders(HttpTransport.java:80)
                at com.squareup.okhttp.internal.http.HttpEngine.readNetworkResponse(HttpEngine.java:917)
                at com.squareup.okhttp.internal.http.HttpEngine.readResponse(HttpEngine.java:757)
                at com.squareup.okhttp.Call.getResponse(Call.java:274)
                at com.squareup.okhttp.Call$ApplicationInterceptorChain.proceed(Call.java:230)
                at com.squareup.okhttp.Call.getResponseWithInterceptorChain(Call.java:201)
                at com.squareup.okhttp.Call.access$100(Call.java:36)
                at com.squareup.okhttp.Call$AsyncCall.execute(Call.java:164)
                at com.squareup.okhttp.internal.NamedRunnable.run(NamedRunnable.java:33)
                at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
                at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
                at java.lang.Thread.run(Thread.java:831)
    

    I'm not sure what I'm doing wrong here, or how to interperet this exception. What's wrong with this websocket, and how can I fix it?