gpt4 book ai didi

http - websockets 必须有心跳吗?

转载 作者:可可西里 更新时间:2023-11-01 15:07:27 28 4
gpt4 key购买 nike

当我读到 websockets 时,通常会提到心跳是必须具备的。 MDN 甚至写了一个 special opcode for heartbeats .

但是心跳是 websockets 的强制性部分吗?我是否必须实现它,否则我的 websockets 将被浏览器或其他一些标准终止?

最佳答案

RFC 6455 ,WebSocket 协议(protocol)的当前引用,定义了一些控制帧来传达有关 WebSocket 的状态:

  • 关闭:0x8
  • 平:0x9
  • 傍:0xA

Ping 和 Pong 用于心跳 并允许您检查客户端是否仍然响应。查看quote下面:

A Ping frame may serve either as a keepalive or as a means toverify that the remote endpoint is still responsive.

但是当客户端得到一个 Ping 时,必须将一个 Pong 发送回服务器。查看quote :

Upon receipt of a Ping frame, an endpoint MUST send a Pong frame inresponse, unless it already received a Close frame. It SHOULDrespond with Pong frame as soon as is practical.

底线

在设计客户端和服务器时,是否支持心跳取决于您。但是如果您需要检查连接是否仍然存在,Ping 和 Pong 帧是执行此操作的标准方法。

请记住,如果发送了 Ping 而未发回 Pong,则一个点可能会认为另一个点不再存在。

关于http - websockets 必须有心跳吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46111656/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com