简体   繁体   中英

Close the Websocket connection using STOMP

Currently, I have a project with Websocket and Stomp as the sub-protocol for messaging, I need to manage the disconnection event and reconnection event on the Websocket. My ultimate goal is to close the WebSocket session with STOMP after 5 seconds if there is no message between server and client . I am quite confused about heart-beat values when set in server and client. For example, I have these heart-beat values set in the client (using Stomp.js ):

stompClient.heartbeat.outgoing = 5000;
stompClient.heartbeat.incoming = 1000;

And here is what I have for the heart-beat values on the server-side (Spring Boot):

config.enableSimpleBroker("/topic")
                .setTaskScheduler(taskScheduler()).setHeartbeatValue(new long[]{5000, 5000});

So I set the value on the server-side it will send a PONG message every 5 seconds, but on the client-side, it expects to receive the message in 1s, but when it waits for more than one second, the connection is still there and the WebSocket is still working.

And if I change one of the values on one side and how it affects the other side? And how I can assert that the connection will be closed in the specific time with these heart-beat values? Thanks very much.

The STOMP specification explains how heart-beating works. An agreement happens between the client and the broker when the connection is created where the largest heart-beat values will be used.

The heart-beat header provides enough information so that each party can find out if heart-beats can be used, in which direction, and with which frequency.

More formally, the initial frames look like:

 CONNECT heart-beat:<cx>,<cy> CONNECTED heart-beat:<sx>,<sy>

For heart-beats from the client to the server:

  • if <cx> is 0 (the client cannot send heart-beats) or <sy> is 0 (the server does not want to receive heart-beats) then there will be none
  • otherwise, there will be heart-beats every MAX(<cx>,<sy>) milliseconds

In the other direction, <sx> and <cy> are used the same way.

In your case the client specifies that it wants to receive heart-beats every 1 second, but the broker can only send pings every 5 seconds (based on the configuration you specified) so the client should only expect pings every 5 seconds (since 5 > 1).

Another example...If you had cx, cy = 5000, 10000 and sx, sy = 15000, 20000 the client would send heart-beats to the broker every 20 seconds (ie MAX(<cx>,<sy>) ) and the broker would send heart-beats to the client every 15 seconds (ie MAX(<sx>,<cy>) ).

Also, keep in mind that a heart-beat and a MESSAGE frame are two different things. If your client doesn't receive a MESSAGE frame the connection can still be valid and the client & broker can & will still exchange heart-beats during this time.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM