Websocket关闭代码:1006节点Android okhttp3 AmazonEc2 [英] Websocket closed code: 1006 Node Android okhttp3 AmazonEc2
问题描述
我在Websocket ws is closed with code: 1006 reason:
I am having an issue with 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错误.
I am getting data from Websockets -> MongoDb and trying to populate into recycler view. The data comes fine if I do log.d but when I populate into the recycler view - it does fine on app launch, and one more time when coming to activity but after that I start getting 1006 error.
NodeJ(服务器)
// 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);
}
}
推荐答案
更奇怪的是,它在失败之前已经工作了一次或两次,这引起了混乱.如果它总是失败并且喜欢一个logcat,那就太好了.最好的方法是在服务器和客户端上记录所有可能的错误/故障,然后查看消息.
The wierd part is that it was working once or twice before failing which was causing confusion. If it always failed and fave a logcat that would have been nice. The best way is to log all the possible errors/failures on server and client and see the message.
首先将日志放入失败
@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`
然后将回收者"视图的内容包装在此
getActivity().runOnUiThread(new Runnable() {
public void run() {
// Poplulate the recycler view here....
}
这篇关于Websocket关闭代码:1006节点Android okhttp3 AmazonEc2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!