'How to find ping/keepalive being sent in okhttp websockets

I'm creating a secured websocket connection between my server using okhttp library. But my websocket connections keep getting closed after 3 minutes. So I added below code to ensure ping/pong:

okHttpClient = new OkHttpClient.Builder()
                    .pingInterval(20, TimeUnit.SECONDS)
                    .build();
            Request request = new Request.Builder().url(freeSwitchHost).build();
            vertoWebSocket = okHttpClient.newWebSocket(request, pstnWebSocketListener);
            okHttpClient.dispatcher().executorService().shutdown();

This is the code I am using to send ping for my server. But unfortunately I am not able to capture any data in pcap that ping is being sent by app. Is there a way to capture the ping being sent by okhttp websocket?

When ping seems to be not working, I tried below code to send keep-alive. But I'm not able to capture that too in pcap as I'm using secured websocket connections.

okHttpClient = new OkHttpClient.Builder()
                        .connectionPool(new ConnectionPool(100, 30, TimeUnit.SECONDS))
                        .build();

This is the code I'm using to send keep-alive. But still same response. I'm not sure whether my websocket connection is sending keep-alive or ping pong. Is there way to identify ping and keep-alive is being sent by my websocket.



Solution 1:[1]

Sadly, I haven't used this library, so I will assume that there are no errors in the code.

I would suggest you use two really useful tools in Wireshark:

  • Packet lengths (Statistics > Packet Lengths)
  • IO Graphs (Statistics > I/O Graphs)

Record a nice Pcap, where the channel is idle so that you can observe the keepalive packets clearly. If this is not an option, you can always use a filter to show you only small packets frame.len<250.

With the packet lengths, I would look for small packets (around 80-159), since the keepalive packets are not that big, and also look at the "Rate" field, as it may help you see if there are patterns.

With the IO Graphs, I would set the interval at something smaller than 100ms and look for peaks 3 seconds apart (which were caused by the keepalive packets).

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 ELKozel