1

My team is building an Android application that will use websockets to communicate with an existing backend. We chose to use the AndroidAsync by Koushik Dutta to handle this communication.
I would like to register a ping to be sent periodically, to check if the connection is still alive. I'm using Wireshark to check the network traffic. This is a screenshot of the result that Wireshark is showing:

Wireshark screenshot

From what I see here, I believe that the ping is being sent, and the pong is being received.
A snippet of my code is:

private void keepAlive() {
    ScheduledExecutorService scheduler =
            Executors.newSingleThreadScheduledExecutor();
    Runnable runnable = new Runnable() {
        public void run() {
            Log.d(TAG, "Pinging...");
            WebSocketHandler.this.webSocket.ping("LALALA");
        }
    };
    pingScheduledFuture = scheduler.scheduleAtFixedRate(runnable, 0, PING_PERIOD,
            TimeUnit.SECONDS);
}

The onPongReceived method just prints into Logcat

@Override
public void onPongReceived(String s) {
    // TODO here I'm aware if connection is still alive
    Log.d(TAG, "Pong received! " + s);
}

However, Pong received! is never printed! Also, if I put a breakpoint there, the app will never stop executing at that point
Anyone has any idea on what may I be missing here?
Best regards and thanks in advance

mjs
  • 2,837
  • 4
  • 28
  • 48
jmm
  • 1,044
  • 2
  • 12
  • 38

2 Answers2

2

I'm not familiar with AsyncSocket but a quick google revealed that you have to register a callback setPongCallback() somewhere for your pong to be received. Are you doing this? You're not showing a lot of code.

ci_
  • 8,594
  • 10
  • 39
  • 63
1

The problem was extremely lame, but here's the solution. I forgot to set the callback to the websocket, like this:

WebSocketHandler.this.webSocket.setPongCallback(WebSocketHandler.this);

And then, the pongs were correctly received.

jmm
  • 1,044
  • 2
  • 12
  • 38