OkHTTP Websocket:Connection上出现意外的蒸汽结束

时间:2015-08-22 22:27:26

标签: android websocket okhttp

我正在尝试连接到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.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)

我不确定我在这里做错了什么,或者如何插入这个例外。这个websocket有什么问题,我该如何解决呢?

2 个答案:

答案 0 :(得分:2)

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

  • 网址参数l,对应于加载事件列表时发回的值。
  • 标题Origin设置为"http://stackexchange.com"

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

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

答案 1 :(得分:1)

最有可能在同一时间发生两件事。
首先,URL包含一个不常用的端口,其次,您正在使用不支持该端口的VPN或代理。
我个人也遇到同样的问题。
我的服务器端口为45860,当时我使用的是pSiphon反过滤VPN。
在这种情况下,只有当服务器的relpy是状态代码大于0的错误时,我的邮递员才报告“连接挂断”。(当从服务器返回一些文本且没有错误代码时,这是很好的选择)
然后,我将服务器上的Web服务端口更改为8080,WOW起作用了!尽管已连接psiphon vpn。
因此,我的建议是,如果可以更改服务器端口,请尝试使用它,或者检查是否存在代理问题。