OkHTTP Websocket:连接意外终止 [英] OkHTTP Websocket: Unexpected end of steam on Connection
问题描述
我正在尝试连接到Stack Exchange聊天Websocket.网络套接字用于接收聊天中的新事件,例如新消息.
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.
以下是用于创建Websocket的代码:
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());
websocket URL的格式如下:
The websocket URL is in this form:
wss://chat.sockets.stackexchange.com/events/16/4b3a8a1f68704b8db35ce9f0915c7c45
WebSocketListener仅收到onFailure
响应,但以下情况除外:
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?
推荐答案
聊天Websocket服务器将忽略该请求,除非同时具有以下两个条件:
The chat websocket server disregards the request unless it has both:
- URL参数
l
,对应于加载事件列表时发回的值. - 标头
Origin
设置为"http://stackexchange.com"
.
- 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();
这篇关于OkHTTP Websocket:连接意外终止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!