问题描述
当我读到关于websockets时,通常会提到心跳是必须的。 MDN甚至写了关于定义了一些控制帧以传达有关WebSocket的状态:
The RFC 6455, the current reference for the WebSocket protocol, defines some control frames to communicate state about the WebSocket:
- 关闭:
0x8
- Ping:
0x9
- Pong:
0xA
- Close:
0x8
- Ping:
0x9
- Pong:
0xA
Ping和Pong用于心跳并允许您检查客户端仍然有回应。请参阅下面的:
Ping and Pong are used for heartbeat and allows you to check if the client is still responsive. See the quote below:
但是当客户端获得Ping时,必须将Pong发送回服务器。请参阅:
But when the client gets a Ping, a Pong must be sent back to the server. See the quote:
底线
在设计两者客户端和服务器时,支持心跳由您决定。但是如果你需要检查连接是否仍然存在,Ping和Pong帧是标准的方法。
Bottom line
When designing both client and server, supporting heartbeats is up to you. But if you need to check if the connection is still alive, Ping and Pong frames are the standard way to do it.
请记住,如果发送了Ping并且不会发回Pong,一个对等体可能会认为另一个对等体不再存在。
Just keep in mind that if a Ping is sent and a Pong is not sent back, one peer may assume that the other peer is not alive anymore.
这篇关于websockets必须有心跳吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!