OkHTTP Websocket:连接上的蒸汽意外结束

OkHTTP Websocket: Unexpected end of steam on Connection

我正在尝试连接到 Stack Exchange 聊天 Websocket。 websocket 用于接收聊天中的新事件,例如新消息。

这是用于创建 Websocket 的代码:

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

websocket URL 是这种形式:

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

WebSocketListener 仅收到 onFailure 响应,但有以下例外:

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.access0(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.access0(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)

我不确定我在这里做错了什么,或者如何解释这个异常。这个 websocket 有什么问题,我该如何解决?

聊天 websocket 服务器忽略请求,除非它同时具有:

  • 一个url参数l,对应加载事件列表时返回的值。
  • A header Origin 设置为 "http://stackexchange.com".

您只需将这些添加到您的请求中:

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

很可能有两件事同时发生。
首先,url 包含一个不常用的端口,其次,您使用的 VPN 或代理不支持该端口。
就我个人而言,我遇到了同样的问题。
我的服务器端口是 45860,我使用的是 pSiphon anti-filter VPN。
在那种情况下,只有当服务器的 relpy 是状态代码大于 0 的错误时,我的邮递员才报告“连接 hang-up”。(当某些文本从服务器返回时没有错误代码,这很好)
然后我将服务器上的 Web 服务端口更改为 8080,哇,成功了!尽管已连接 psiphon vpn。
因此,我的建议是,如果可以更改服务器端口,那么尝试一下,或者检查是否存在代理问题。