websockets必须有心跳吗? [英] Must websockets have heartbeats?

查看:231
本文介绍了websockets必须有心跳吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我读到关于websockets时,通常会提到心跳是必须的。 MDN甚至写了关于心跳的特殊操作码

When I read about websockets, heartbeats are usually mentioned as a must have. MDN even writes about a special opcode for heartbeats.

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

But are heartbeats a mandatory part of websockets? Do I have to implement it or else my websockets will be terminated by the browsers or some other standards?

推荐答案

WebSocket协议的当前参考 RFC 6455 定义了一些控制帧以传达有关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帧可以用作keepalive,也可以用作
的方式验证远程端点是否仍然响应。

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

但是当客户端获得Ping时,必须将Pong发送回服务器。请参阅报价

But when the client gets a Ping, a Pong must be sent back to the server. See the quote:


收到Ping帧后,端点必须在
响应中发送Pong帧,除非它已收到Close帧。它应该尽快用
用Pong框架回复。

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



底线



在设计两者客户端和服务器时,支持心跳由您决定。但是如果你需要检查连接是否仍然存在,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必须有心跳吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆