I am trying to send data and receive an echo of the same via a websocket. When I try to send it via a background thread, I receive no echo. What could be wrong? Is it incorrect to send Data via a background thread? If so, why?
Please check my code and the associated logs attached with it.
private final class EchoWebSocketListener extends WebSocketListener {
private static final int NORMAL_CLOSURE_STATUS = 1000;
@Override
public void onOpen(final WebSocket webSocket, Response response) {
class SendData extends AsyncTask {
@Override
protected void onPreExecute(){
Log.d("Websocket","In Pre-execute");
webSocket.send("In pre-execute");
}
@Override
protected Object doInBackground(Object[] objects) {
Log.d("AsyncTask","In doInBackground");
//The below statement receives no echo
webSocket.send("doInBackground");
Log.d("AsyncTask","In END of doInBackground");
return null;
}
}
new SendData().execute();
webSocket.send(ByteString.decodeHex("1245"));
webSocket.close(NORMAL_CLOSURE_STATUS, "Goodbye !");
}
@Override
public void onMessage(WebSocket webSocket, String text) {
Log.i("Receiving : ",text);
}
@Override
public void onMessage(WebSocket webSocket, ByteString bytes) {
Log.i("Receiving bytes : " , bytes.hex());
}
@Override
public void onClosing(WebSocket webSocket, int code, String reason) {
webSocket.close(NORMAL_CLOSURE_STATUS, null);
Log.i("Closing : " , code + " / " + reason);
}
@Override
public void onFailure(WebSocket webSocket, Throwable t, Response response) {
Log.i("Error", "Error in WebsocketEcho");
}
}
Logs:
D/AsyncTask: In Pre-execute
D/AsyncTask: In doInBackground
D/AsyncTask: In END of doInBackground
I/Receiving :: In pre-execute
I/Receiving bytes :: 1245
I/Closing :: 1000 / Goodbye !