Javascript 从浏览器发送 websocket ping/pong 帧
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10585355/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Sending websocket ping/pong frame from browser
提问by danny
I keep reading about ping/pong messages in websockets to keep the connection alive, but I'm not sure what they are. Is it a distinct frame type? (I don't see any methods on a javascript WebSocket object in chrome related to ping-pong). Or is it just a design pattern (e.g. I literally send "ping" or any other string to the server and have it respond). Is ping-pong at all related to continuation frames?
我一直在阅读有关 websockets 中的 ping/pong 消息以保持连接有效,但我不确定它们是什么。它是一种独特的框架类型吗?(我在 chrome 中的 javascript WebSocket 对象上没有看到与乒乓相关的任何方法)。或者它只是一种设计模式(例如,我从字面上向服务器发送“ping”或任何其他字符串并让它响应)。乒乓球与连续帧有关吗?
The reason I ask is I'm using a python framework that runs behind Mongrel2, so I'm wondering if there's a way to send Mongrel2 a specific ping/pong message that would tell it to keep the connection alive without my python app needing to worry about it. Analogous to a having a separate HTTP method for it, I guess. And I imagine a dedicated ping/pong message frame could be simpler (less load on server and network) than the string "ping", though that probably wouldn't matter too much.
我问的原因是我使用的是在 Mongrel2 后面运行的 python 框架,所以我想知道是否有办法向 Mongrel2 发送特定的 ping/pong 消息,告诉它保持连接活动,而我的 python 应用程序不需要担心它。我想类似于有一个单独的 HTTP 方法。我想一个专用的 ping/pong 消息帧可能比字符串“ping”更简单(服务器和网络上的负载更少),尽管这可能不会太重要。
EDIT: I just looked at RFC 6455and it looks like Ping and Pong are definitely control frame types with their own opcodes. So how do I send a Ping frame from javascript in Chrome?
编辑:我刚刚查看了RFC 6455,看起来 Ping 和 Pong 绝对是具有自己操作码的控制帧类型。那么如何在 Chrome 中从 javascript 发送 Ping 帧呢?
采纳答案by kanaka
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. There was discussion about creating a Javascript ping/pong APIfor this. There is a possibility that pings may be configurable/detectable in the future, but it is unlikely that Javascript will be able to directly send and receive ping/pong frames.
没有用于发送 ping 帧或接收 pong 帧的 Javascript API。这要么受您的浏览器支持,要么不支持。也没有 API 来启用、配置或检测浏览器是否支持和正在使用 ping/pong 帧。有关于为此创建 Javascript ping/pong API 的讨论。将来有可能 ping 是可配置/可检测的,但 Javascript 不太可能直接发送和接收 ping/pong 帧。
However, if you control both the client and server code, then you can easily add ping/pong support at a higher level. You will need some sort of message type header/metadata in your message if you don't have that already, but that's pretty simple. Unless you are planning on sending pings hundreds of times per second or have thousands of simultaneous clients, the overhead is going to be pretty minimal to do it yourself.
但是,如果您同时控制客户端和服务器代码,则可以轻松地在更高级别添加 ping/pong 支持。如果您还没有,您的消息中将需要某种消息类型标头/元数据,但这很简单。除非您计划每秒发送数百次 ping 或同时拥有数千个客户端,否则您自己完成的开销将非常小。
回答by moka
Ping is meant to be sent only from server to client, and browser should answer as soon as possible with Pong OpCode, automatically. So you have not to worry about that on higher level.
Ping 只能从服务器发送到客户端,浏览器应尽快自动回复 Pong OpCode。因此,您不必担心更高级别的问题。
Although that not all browsers support standard as they suppose to, they might have some differences in implementing such mechanism, and it might even means there is no Pong response functionality. But personally I am using Ping / Pong, and never saw client that does not implement this type of OpCode and automatic response on low level client side implementation.
尽管并非所有浏览器都像他们想象的那样支持标准,但它们在实现这种机制方面可能存在一些差异,甚至可能意味着没有 Pong 响应功能。但我个人正在使用 Ping/Pong,从未见过没有在低级客户端实现上实现这种类型的 OpCode 和自动响应的客户端。