简体   繁体   English

使用 STOMP 关闭 Websocket 连接

[英]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.目前,我有一个项目,Websocket 和 Stomp 作为消息传递的子协议,我需要管理 Websocket 上的断开事件和重新连接事件。 My ultimate goal is to close the WebSocket session with STOMP after 5 seconds if there is no message between server and client .如果服务器和客户端之间没有消息,我的最终目标是在 5 秒后用 STOMP 关闭 WebSocket session 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 ):例如,我在客户端中设置了这些心跳值(使用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):这是我在服务器端(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.所以我在服务器端设置了这个值,它会每5秒发送一条PONG消息,但是在客户端,它希望在1s内收到消息,但是当它等待超过一秒时,连接仍然在那里,WebSocket 仍在工作。

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. STOMP 规范解释了心跳的工作原理。 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. heart-beat header 提供了足够的信息,以便各方可以了解是否可以使用心跳、在哪个方向以及以何种频率使用。

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如果<cx>为 0(客户端无法发送心跳)或<sy>为 0(服务器不想接收心跳),则不会有
  • otherwise, there will be heart-beats every MAX(<cx>,<sy>) milliseconds否则,每隔MAX(<cx>,<sy>)毫秒会有一次心跳

In the other direction, <sx> and <cy> are used the same way.在另一个方向上, <sx><cy>的使用方式相同。

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).在您的情况下,客户端指定它希望每 1 秒接收一次心跳,但代理只能每 5 秒发送一次 ping(基于您指定的配置),因此客户端应该只期望每5秒发送一次 ping(因为 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>) ).另一个例子...如果您有cx, cy = 5000, 10000sx, sy = 15000, 20000 ,客户端将每 20 秒向代理发送一次心跳(即MAX(<cx>,<sy>) )和代理将每 15 秒向客户端发送一次心跳(即MAX(<sx>,<cy>) )。

Also, keep in mind that a heart-beat and a MESSAGE frame are two different things.另外,请记住,心跳和MESSAGE帧是两个不同的东西。 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.如果您的客户端没有收到MESSAGE帧,则连接仍然有效,并且客户端和代理在此期间仍然可以交换心跳。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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