最新消息:VPS服务器又从fzhost.net换回Linode了,主题仍用朋友推荐的大前端D8

【整理】websocket中的pong是什么意思

Web_Technology crifan 127浏览 0评论

daltoniam/Starscream · GitHub

中看到:

“Optional: websocketDidReceivePong (required protocol: WebSocketPongDelegate)”

不懂啥是Pong

搜:

websocket pong whatis

websocket what is pong

参考:

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.

http://django-websocket-redis.readthedocs.org/en/latest/heartbeats.html

“Sending and receiving heartbeat messages — django …

django-websocket-redis.readthedocs.org/en/…/heartbeats.html

The Websocket protocol implements so called PING/PONG messages to keep Websockets alive, even behind proxies, firewalls and load-balancers. The server  …”

websocket pong Ping

Ping and Pong frames- The WebSocket API

http://www.w3.org/TR/2011/CR-websockets-20111208/

http://www.w3.org/TR/2011/CR-websockets-20111208/#ping-and-pong-frames

6 Ping and Pong frames

The WebSocket protocol specification defines Ping and Pong frames that can be used for keep-alive, heart-beats, network status probing, latency instrumentation, and so forth. These are not currently exposed in the API.

User agents may send ping and unsolicited pong frames as desired, for example in an attempt to maintain local network NAT mappings, to detect failed connections, or to display latency metrics to the user. User agents must not use pings or unsolicited pongs to aid the server; it is assumed that servers will send solicit pongs whenever appropriate for the server’s needs.

Websocket – Ping Pong Signals

Websocket – Ping Pong Signals

If you’re working with Websockets, you might have experienced problems with connections suddenly being dropped. One reason for this could be the lack of traffic during a certain amount of time, and that can be solved by sending ping and pong signals back and forth between the client and the websocket server, just to keep the connection alive.
To understand how true ping pong works over Websocket, you need to be aware of the structure of a so called Websocket frame. As usually, I won’t get too technical (you can read the 
Websocket RFC for details). Instead I’ll dumb it down for you.
The actual readable and/or interpretable content that you want to send over the websocket protocol, is called a websocket "message". Every message that is about to get sent from the server to the client, is processed first and turned into a part of a websocket "frame". You can almost compare a frame to a standard HTML document that has a visible "body" containing content and an invisible "head" containing different properties. (Except the websocket frame isn’t readable.) The frame’s head contains a property that tells what kind of message is being sent. This property can, for example, be set to "text message" which is what you’d usually want. But it can also be set to "ping" or "pong", which is exactly what we want in this case.
But no, we can’t use ping/pong frames. Even though it’s absolutely possible to produce a pong message on the server side, it’s not possible to produce a ping message on the client side, because the JavaScript Websocket API doesn’t let anyone set these parameters. Today, it’s only possible to send "text messages" from a web browser. (Though if you use websockets between two servers, you can do as you please because no API is holding you back.)
Therefore, you have to make the server understand when a text message should be interpreted as a ping signal. But once you have an input interpreter, that is easy.

I suggest that you don’t just check if the message says "ping". What if a user actually wants to use the text "ping" in a text message and not as an actual ping signal? Instead, you could give every client-to-server message a head and a body by formatting it as JSON.

{"type":"text","content":"ping"}

{"type":"ping"}

This method also prevents other weird problems from happening. If you don’t use JSON (or possibly XML, I haven’t tried), your browser can give you error messages saying "Unexpected character" about the first character of the message.

Setting up a ping signal on the client side is actually also simple. You can make the client send a signal regularly, but to avoid unnecessary messages, you can put the current time in a variable every time you send a message, and then check – every second – if a certain amount of seconds has passed since the last message was sent. In the example below, a ping message is sent if 20 seconds has passed since the last message. (You will have to adapt the snippet after your own code, but you’ll understand the idea.)

setInterval(
function(){
var interval = 20 * 1000;
var currentTime = new Date().getTime();
if( ! app.lastMessageSent || app.lastMessageSent < currentTime – interval ){
app.sendToServer("Ping");
app.lastMessageSent = currentTime;
}
},
1000
);

The whole thing is set up in five minutes, and could be a good way to avoid unnecessary problems. And trust me, if you work with websockets, you already have enough of them.

RFC 6455 – The WebSocket Protocol

【总结】

Websocket协议中规定的特殊名词:

  • Ping和Pong是websocket的两端,比如服务器端和app端,之间发送的特殊数据,用于检测对方是否还存在和有效
  • Ping可以包含数据-》很多具体实现都是把当前的时间放进去
  • 在一端收到了Ping的时候,该端点必须发送Pong给对方,同时把Ping传递过来的数据原封不动的发送回去

一般很多的Websocket的库都已经封装好了Ping和Pong了

比如:

Swift的daltoniam/Starscream · GitHub中的websocketDidReceivePong

详见:

【已解决】Swift中建立websocket连接监听端口收发消息

转载请注明:在路上 » 【整理】websocket中的pong是什么意思

发表我的评论
取消评论

表情

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

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
15 queries in 0.244 seconds, using 9.75MB memory