Websocket 关闭代码:1006 Node Android okhttp3 AmazonEc2
Posted
技术标签:
【中文标题】Websocket 关闭代码:1006 Node Android okhttp3 AmazonEc2【英文标题】:Websocket closed code: 1006 Node Android okhttp3 AmazonEc2 【发布时间】:2020-09-06 16:28:58 【问题描述】:我在使用 Websocket ws is closed with code: 1006 reason:
时遇到问题
环境
Amazon EC2 Instance : t2.micro Ubuntu 18.04
Node : v12.16.3
Websocket : https://github.com/websockets/ws : 7.3.0
MongoDb : shell version v4.2.6
MongoDriver : NodeJs 3.5.7
Okhhtp3 : implementation 'com.squareup.okhttp3:okhttp:4.6.0'
我正在从 Websockets -> MongoDb 获取数据并尝试填充到回收站视图中。如果我执行 log.d,数据会很好,但是当我填充到回收器视图时 - 它在应用程序启动时运行良好,并且在开始活动时再运行一次,但之后我开始收到 1006 错误。
NodeJs(服务器)
// Websocket variables
const WebSocket = require('ws');
const wss = new WebSocket.Server( port: 8000 );
console.log('Websocket active on port 8000...');
// Connect To Mongo
MongoClient.connect(url, useUnifiedTopology: true, authSource: 'admin' , function (err, client)
if (err)
throw err;
const db = client.db(dbName);
// New Connection
wss.on('connection', function connection(ws)
// Message Received
ws.on('message', function incoming(message)
// Json Parse String To Access Child Elements
var iJson = JSON.parse(message);
const c = db.collection('chats');
c.find( "owner": iJson.owner_id ).toArray(function (err, docs)
ws.send(JSON.stringify(docs));
);
);
// Connection Closed
ws.on('close', function close(code, reason)
console.log('ws is closed with code: ' + code + ' reason: ' + reason);
);
);
);
Android(客户端)
private WebSocket webSocket;
// Initiate The Socket Connection
private void initiateSocketConnection()
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder().url(WebSocketClient.SERVER_PATH).build();
webSocket = client.newWebSocket(request, new SocketListener());
// Private WebSocketListener Class
private class SocketListener extends WebSocketListener
public SocketListener()
super();
@Override
public void onClosed(@NotNull WebSocket webSocket,
int code,
@NotNull String reason)
super.onClosed(webSocket,
code,
reason);
Log.d("TAG",
"onClosed: " + code + "-" + reason);
@Override
public void onFailure(@NotNull WebSocket webSocket,
@NotNull Throwable t,
okhttp3.@org.jetbrains.annotations.Nullable Response response)
super.onFailure(webSocket,
t,
response);
@Override
public void onMessage(@NotNull WebSocket webSocket,
@NotNull String text)
super.onMessage(webSocket,
text);
String chat = "\"chat\":" + text + "";
Log.d("TAG",
"onMessage: " + chat);
lIndividualChatList.clear();
try
JSONObject jsonObject = new JSONObject(chat.trim());
// Check If User Array Has Anything
JSONArray returnArray = jsonObject.getJSONArray("chat");
for (int l = 0; l < returnArray.length(); l++)
if (returnArray.length() > 0)
// Get The Json Object
JSONObject returnJSONObject = returnArray.getJSONObject(l);
// Get Details
String owner = returnJSONObject.optString("owner");
lIndividualChatList.add(new IndividualListModel(owner));
// Use The Adapter To Populate The Recycler View
aIndividualChatList = new IndividualListAdapter(lIndividualChatList);
rvList.setAdapter(aIndividualChatList);
aIndividualChatList.notifyDataSetChanged();
catch (JSONException e)
e.printStackTrace();
@Override
public void onOpen(@NotNull WebSocket webSocket,
okhttp3.@NotNull Response response)
super.onOpen(webSocket,
response);
【问题讨论】:
为帮助解决此问题,请设置ws.on('error',...)
事件处理程序。你有什么有趣的事情吗?
ws.on('error', function(e) console.log("错误发生" +e); );不开火
@O.Jones 解决了感谢您指出记录更多错误,我在服务器和客户端记录了尽可能多的错误...谢谢
【参考方案1】:
奇怪的部分是它在失败之前工作了一两次,这导致了混乱。如果它总是失败并喜欢一个本来不错的 logcat。最好的方法是在服务器和客户端上记录所有可能的错误/故障并查看消息。
在失败时首先将日志放入内部
@Override
public void onFailure(@NotNull WebSocket webSocket, @NotNull Throwable t, okhttp3.@org.jetbrains.annotations.Nullable Response response)
super.onFailure(webSocket, t, response);
Log.d("TAG", "websocket failure: " + t + response);
这给出了以下信息:
: `websocket failure: android.view.ViewRootImpl$CalledFromWrongThreadException:
Only the original thread that created a view hierarchy can touch its views.null`
然后将recycler view的population包裹在这个
getActivity().runOnUiThread(new Runnable()
public void run()
// Poplulate the recycler view here....
【讨论】:
以上是关于Websocket 关闭代码:1006 Node Android okhttp3 AmazonEc2的主要内容,如果未能解决你的问题,请参考以下文章
Tomcat WebSocket 连接关闭,代码:1006 问题
当我在 Open*** 后面时,浏览器在建立 WebSocket 连接一秒后关闭并显示错误 1006
Go websocket错误:关闭1006(异常关闭):意外EOF
Node.js "ws" websocket 服务器在约 30 秒后随机断开客户端,错误 1006