Search code examples
javaspring-bootwebsocketspring-websocketstomp

Detect websocket connection break on internet connection loss stomp client java


Java stomp client does not respond on internet connection loss. Is there any way to get an exception inside handleTransportError method on internet connection loss or socket disconnection?

public class SocketHandler extends StompSessionHandlerAdapter{

@Override
public void handleTransportError(){
 //Expected error here
}

@Override
public void handleException(){

}

@Override
public void afterConnected(){

}

Solution

  • The TCP protocol can be slow to notice a connection loss. That is why the STOMP 1.1 protcol introduced the Heart-beat feature. Heart-beating can optionally be used to test the healthiness of the underlying TCP connection and to make sure that the remote end is alive and kicking.

    Depending on what is negotiated with heart-beating at initial connection time, the client or server may be required to send periodic "heart beat" messages. If your client doesn't receive a heart-beat from the server within the prescribed time period it assumes that the connection is lost.