tcp - WebSockets ping/pong,为什么不是 TCP keepalive?

标签 tcp websocket keep-alive

WebSockets have the option发送 ping 到另一端,另一端应该用 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.

TCP offers something similar以keepalive的形式:

[Y]ou send your peer a keepalive probe packet with no data in it and the ACK flag turned on. You can do this because of the TCP/IP specifications, as a sort of duplicate ACK, and the remote endpoint will have no arguments, as TCP is a stream-oriented protocol. On the other hand, you will receive a reply from the remote host (which doesn't need to support keepalive at all, just TCP/IP), with no data and the ACK set.

我认为 TCP keepalive 更有效,因为它可以在内核中处理,而不需要将数据传输到用户空间,解析 websocket 帧,制作响应帧,然后将其交还给内核传播。它还减少了网络流量。

此外,WebSockets are explicitly specified始终运行在 TCP 上;它们与传输层无关,因此 TCP keepalive 始终可用:

The WebSocket Protocol is an independent TCP-based protocol.

那么为什么人们会想要使用 WebSocket ping/pong 而不是 TCP keepalive?

最佳答案

TCP keepalive存在的问题是:

  1. 默认关闭。
  2. 默认情况下,它每两小时运行一次,而不是像 Ping/Pong 协议(protocol)提供的那样按需运行。
  3. 它在代理之间而不是端到端运行。
  4. 正如@DavidSchwartz 所指出的,它在 TCP 堆栈之间运行,而不是在应用程序之间运行,因此它不会告诉我们应用程序是否处于事件状态。

与 WebSockets ping/pong 的比较没有意义。 TCP keepalive 是自动的,并且在启用时是定时的,而 WebSocket ping/pong 是根据应用程序的要求执行的。

关于tcp - WebSockets ping/pong,为什么不是 TCP keepalive?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23238319/

相关文章:

websocket - 是否可以捕获 signalR 连接断开事件?错误 : Connection disconnected with error webSocket closed with status code: 1006

node.js - meteor 兼容性

java - 为什么 Spring Cloud Stream 应用程序启动项目中没有可用的 websocket 源?

nginx - Nginx 热配置重新加载 (SIGHUP) 期间,进行中的 HTTP 请求失败

sockets - Socket.IOControl dotnet 核心 Linux 不工作

java - 是打开不同的端口更好,还是打开一个带有标识符的端口更好(或者其他)?

powershell - 如何在 Powershell 中自动检查 Telnet 端口?`

sockets - TCP套接字的传输时间

tcp - 使用 Scapy 发送 tcp payload : Why a byte is eaten?

macos - 没有网络连接时启动 NetworkState