使用 STOMP 关闭 Websocket 连接
Close the Websocket connection using STOMP
目前,我有一个以Websocket和Stomp作为消息传递子协议的项目,我需要管理Websocket上的断开连接事件和重连事件。 我的最终目标是如果服务器和客户端之间没有消息,则在 5 秒后关闭与 STOMP 的 WebSocket 会话。在服务器和客户端中设置时,我对心跳值感到很困惑。例如,我在客户端中设置了这些心跳值(使用 Stomp.js
):
stompClient.heartbeat.outgoing = 5000;
stompClient.heartbeat.incoming = 1000;
这是我在服务器端(Spring 引导)的心跳值:
config.enableSimpleBroker("/topic")
.setTaskScheduler(taskScheduler()).setHeartbeatValue(new long[]{5000, 5000});
所以我在服务器端设置的值是每5秒发送一个PONG消息,但是在客户端,它期望在1s内收到消息,但是当它等待超过一秒时,连接仍然存在,WebSocket 仍在工作。
如果我更改一侧的其中一个值,它会如何影响另一侧?我如何断言连接将在 特定时间 与这些心跳值关闭?非常感谢。
STOMP specification 解释心跳是如何工作的。创建连接时,客户端和代理之间会达成协议,其中将使用 最大 心跳值。
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.
在您的情况下,客户端指定它希望每 1 秒接收一次心跳,但代理只能每 5 秒发送一次 ping(根据您指定的配置),因此客户端应该只期望每隔 5 秒(因为 5 > 1)。
另一个例子...如果你有 cx, cy = 5000, 10000
和 sx, sy = 15000, 20000
客户端将每 20 秒向代理发送心跳(即 MAX(<cx>,<sy>)
),代理将发送每 15 秒向客户端发送一次心跳(即 MAX(<sx>,<cy>)
)。
此外,请记住心跳和 MESSAGE
帧是两种不同的东西。如果您的客户端没有收到 MESSAGE
帧,连接仍然有效,并且客户端和代理可以并且仍将在此期间交换心跳。
目前,我有一个以Websocket和Stomp作为消息传递子协议的项目,我需要管理Websocket上的断开连接事件和重连事件。 我的最终目标是如果服务器和客户端之间没有消息,则在 5 秒后关闭与 STOMP 的 WebSocket 会话。在服务器和客户端中设置时,我对心跳值感到很困惑。例如,我在客户端中设置了这些心跳值(使用 Stomp.js
):
stompClient.heartbeat.outgoing = 5000;
stompClient.heartbeat.incoming = 1000;
这是我在服务器端(Spring 引导)的心跳值:
config.enableSimpleBroker("/topic")
.setTaskScheduler(taskScheduler()).setHeartbeatValue(new long[]{5000, 5000});
所以我在服务器端设置的值是每5秒发送一个PONG消息,但是在客户端,它期望在1s内收到消息,但是当它等待超过一秒时,连接仍然存在,WebSocket 仍在工作。
如果我更改一侧的其中一个值,它会如何影响另一侧?我如何断言连接将在 特定时间 与这些心跳值关闭?非常感谢。
STOMP specification 解释心跳是如何工作的。创建连接时,客户端和代理之间会达成协议,其中将使用 最大 心跳值。
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>)
millisecondsIn the other direction,
<sx>
and<cy>
are used the same way.
在您的情况下,客户端指定它希望每 1 秒接收一次心跳,但代理只能每 5 秒发送一次 ping(根据您指定的配置),因此客户端应该只期望每隔 5 秒(因为 5 > 1)。
另一个例子...如果你有 cx, cy = 5000, 10000
和 sx, sy = 15000, 20000
客户端将每 20 秒向代理发送心跳(即 MAX(<cx>,<sy>)
),代理将发送每 15 秒向客户端发送一次心跳(即 MAX(<sx>,<cy>)
)。
此外,请记住心跳和 MESSAGE
帧是两种不同的东西。如果您的客户端没有收到 MESSAGE
帧,连接仍然有效,并且客户端和代理可以并且仍将在此期间交换心跳。