Search code examples
javaspring-bootwebsocketpingspring-websocket

how to implement ping pong in java spring websocket


As per the documentation here https://www.rfc-editor.org/rfc/rfc6455#page-37, a websocket must respond to a ping request with pong.

I have implemented spring boot websocket to handle the websocket connections from clients.

I have a websocket client implemented which sends ping frames periodically.

I found it here https://stackoverflow.com/a/24438328 that I cannot process ping messages in java.

I have queries regarding this -

  1. Should I have to handle these ping requests in java spring-boot websocket?
  2. if yes how could I implement this in spring boot application and respond to a ping request with pong?

Solution

  • Extend class TextWebSocketHandler and write your logic in handleTextMessage method. If you are looking for more generic approach look into WebSocketHandler interface. But as Antoniossss suggested ping pong is already implemented on protocol level for checking if connection is still alive.