Reputation: 4082
I am having an issue with Websocket ws is closed with code: 1006 reason:
Environment
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'
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.
NodeJs (Server)
// 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 (Client)
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,
[email protected] 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);
}
}
Upvotes: 1
Views: 1172
Reputation: 4082
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.
First Put The Log Inside On Failure
@Override
public void onFailure(@NotNull WebSocket webSocket, @NotNull Throwable t, [email protected] Response response) {
super.onFailure(webSocket, t, response);
Log.d("TAG", "websocket failure: " + t + response);
}
That gave the following message :
: `websocket failure: android.view.ViewRootImpl$CalledFromWrongThreadException:
Only the original thread that created a view hierarchy can touch its views.null`
Then Wrap the popluation of recycler view in this
getActivity().runOnUiThread(new Runnable() {
public void run() {
// Poplulate the recycler view here....
}
Upvotes: 0