0

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 !
vss
  • 567
  • 2
  • 7
  • 18
  • Why you are using socket in a background thread? – Muhammad Saad Rafique Nov 10 '17 at 10:28
  • I'm trying to fetch some data from some source running in Background and sending it via the webSocket. If I try to do that on the main thread, I get an error saying too many frames, some frames were skipped. I do not need anything to be displayed on the screen while this is running, hence I thought I could try doing this in the background. Please correct me if I am wrong – vss Nov 10 '17 at 11:12
  • Buddy feth that data via async task once you get data after that use it and give it to socket – Muhammad Saad Rafique Nov 10 '17 at 11:44

1 Answers1

0

Async task when finishes close the thread. So the thread on which your socket is closed with completion of AsyncTask. Use your socket out of asyncTask then it will work. For further details study execution of async task and threadpoolexecutor.

You can find more details here:

https://developer.android.com/reference/android/os/AsyncTask.html

Muhammad Saad Rafique
  • 3,158
  • 1
  • 13
  • 21