最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

[整理]websocket的ping和pong以及ping的最佳间隔时间

工作和技术 crifan 20376浏览 0评论

web browser websocket   ping

浏览器 websocket   ping

Can I use… Support tables for HTML5, CSS3, etc

WebSocket – 维基百科,自由的百科全书

javascript – Sending websocket ping/pong frame from browser – Stack Overflow

8.4 WebSocket ping和pong-HTML5 WebSocket权威指南 – 图文摘

8.4 WebSocket ping和pong

连接可能因为许多你无法控制的原因而意外关闭。任何Web应用程序都应该能够很好地处理间歇连接性并正确地恢复。然而,有些连接关闭的原因是可以且应当避免的。可以避免的常见连接丢失原因是TCP级别的空闲,这会影响WebSocket连接。

说明 > 因为WebSocket连接处于TCP连接的上层,发生在TCP级别的连接问题会影响WebSocket连接。

在客户端和WebSocket服务器之间的全双工连接中,有时候连接上可能没有数据流。在这个时候,网络中介可能中止连接。具体地说,不知道“始终打开”连接的网络组件有时候会关闭不活跃的TCP连接,从而关闭WebSocket连接。例如,代理服务器和家庭路由器有时候会关闭它们认为是空闲的连接。WebSocket协议支持用于连接健康检查和保持连接打开状态的ping和pong。

使用WebSocket ping和pong能够保持连接打开,为数据流动做好准备。ping和pong可以从打开的WebSocket连接的任一端发起。WebSocket协议支持客户端发起和服务器发起的ping和pong。浏览器或服务器(也可以是两者)都可以在合适的时间间隔内发起ping和pong,保持连接活跃。注意,我们说的是浏览器而不是WebSocket客户端:正如我们在第2章中所提到的,WebSocket API目前不支持客户端发起的ping和pong。虽然浏览器可能根据自己的持续性和健康检查策略发起ping和pong,但大部分ping和pong是服务器发起的;WebSocket客户端可以用pong响应ping。浏览器和服务器也可以在没有接收ping的情况下发出pong,这为你提供了保持连接活跃时的灵活性。你所使用的时间间隔根据应用程序的受众和通过WebSocket连接的数据流速而定。保守地说,每30秒发送一个pong应该能够保持大部分连接的活跃性,但是更低的频率能够节约带宽和服务器资源。

WebSocket的ping与pong的java实现,以及浏览器一pong就关闭链接毛病解决_flash胜龙_新浪博客

“网上大多数例子给的都是echo server,也就是当websocket客户端链接进来以后,客户端发一条消息,服务器就把这条消息原班不动地返回去。而且绝大多数没有进行ping、pong判断。于是就出现一个问题:当电脑浏览器发送pong帧的时候,由于内容为空,于是服务器将空内容转发回去,导致客户端浏览器以为是错误的帧类型,发送关闭信息进行error关闭。

  一开始还误以为是浏览器发送ping帧,服务器没有回复pong帧导致的,最后用wireshark抓包解析才发现浏览器发送的是pong帧……居然有单独发送pong帧,给跪了,还以为一ping一pong很合拍呢。网上的资料也证实了这个说法,javascript中并没有发送ping和pong的API,全靠浏览器自己的行为:

  浏览器的WebSocket不支持pingpong吗? 

 “我这只有个go的例子 https:// github.com /gorilla /websocket /blob /master /examples /chat /conn.go 只能server端主动去ping,客户端pong,反之不行,看起来似乎只能起个心跳的作用 

  Sending websocket ping/pong frame from browser

 “……There is no Javascript API to send ping frames or receive pong frames. This is either supported by your browser, or not. There is also no API to enable, configure or detect whether the browser supports and is using ping/pong frames.……

  于是,实际测试中,IE11会在双方都不收发消息的情况下每隔30秒发一个pong帧,iOS 7上的safari和Android 5.0自带浏览器则不会发送任何pong帧。"

Websocket – Ping Pong Signals

RFC 6455 – The WebSocket protocol

5.5.2.  Ping

   The Ping frame contains an opcode of 0x9.

   A Ping frame MAY include "Application data".

   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.  Pong frames are

   discussed in Section 5.5.3.

   An endpoint MAY send a Ping frame any time after the connection is

   established and before the connection is closed.

   NOTE: A Ping frame may serve either as a keepalive or as a means to

   verify that the remote endpoint is still responsive.

5.5.3.  Pong

   The Pong frame contains an opcode of 0xA.

   Section 5.5.2 details requirements that apply to both Ping and Pong

   frames.

   A Pong frame sent in response to a Ping frame must have identical

   "Application data" as found in the message body of the Ping frame

   being replied to.

   If an endpoint receives a Ping frame and has not yet sent Pong

   frame(s) in response to previous Ping frame(s), the endpoint MAY

   elect to send a Pong frame for only the most recently processed Ping

   frame.

"

关于ping和pong的间隔:

很多人说是常见的是30秒

比如IE11每隔30秒

websocket ping interval

tcp – How to decide the Ping interval for websocket connection – Stack Overflow

-》WebSocket-Node/WebSocketServer.js at master · theturtle32/WebSocket-Node

一般用20秒,就够了:

不会太频繁,也不会间隔太长

WebSockets ping/pong, why not TCP keepalive? – Stack Overflow

855906 – Set pingInterval on websocket

Firefox中,用的是55秒

x/net/websocket: server should ping on an interval · Issue #5958 · golang/go

中也是2分钟=120秒

[WEBSOCKET_SPEC-176] Specification does not specify who should send Pings and how often – Java.net JIRA

Chrome和Firefox会把炒过30秒没活动的websocket连接中断掉

-》所以ping的间隔最好小于30秒

-》20秒是个不错的值,即不太频繁,也不太少

Jef Claes: Keeping WebSockets alive

5秒一次

You might not need a WebSocket | Hacker News

低到20秒,高到2分钟=120秒

[总结]

那就还是用20秒吧

-》因为一般的环境中超时时间很多都是30秒:

  • TCP的超时时间是30秒
  • 一些浏览器,比如Firefox,Chrome,也会把超过30秒不活动的websocket连接中断掉,

—》所以选择了这个,小于30秒的时间

-》20秒是个不错的值,即不太频繁,也不太少

转载请注明:在路上 » [整理]websocket的ping和pong以及ping的最佳间隔时间

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
80 queries in 0.167 seconds, using 22.21MB memory