从浏览器发送 websocket ping/pong 帧 [英] Sending websocket ping/pong frame from browser

查看:161
本文介绍了从浏览器发送 websocket ping/pong 帧的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在阅读有关 websockets 中的 ping/pong 消息以保持连接有效,但我不确定它们是什么.它是一种独特的框架类型吗?(我在 chrome 中的 javascript WebSocket 对象上没有看到与乒乓相关的任何方法).或者它只是一种设计模式(例如,我实际上将ping"或任何其他字符串发送到服务器并让它响应).乒乓球与连续帧有关吗?

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?

我问的原因是我使用的是在 Mongrel2 后面运行的 python 框架,所以我想知道是否有办法向 Mongrel2 发送特定的 ping/pong 消息,告诉它在没有我的 python 的情况下保持连接活跃应用程序需要担心它.我想类似于为它提供一个单独的 HTTP 方法.而且我认为专用的 ping/pong 消息帧可能比字符串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.

我刚刚查看了 RFC 6455 和看起来 Ping 和 Pong 绝对是具有自己操作码的控制帧类型.那么如何从 Chrome 中的 javascript 发送 Ping 帧?

I just looked at RFC 6455 and 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?

推荐答案

没有用于发送 ping 帧或接收 pong 帧的 Javascript API.这要么受您的浏览器支持,要么不受支持.也没有用于启用、配置或检测浏览器是否支持和正在使用乒乓帧的 API.有 讨论为此创建 Javascript ping/pong API.将来有可能 ping 是可配置/可检测的,但 Javascript 不太可能直接发送和接收 ping/pong 帧.

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 API for 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 支持.如果您还没有,您的消息中将需要某种消息类型标头/元数据,但这很简单.除非您计划每秒发送数百次 ping 或同时拥有数千个客户端,否则您自己完成的开销将非常小.

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.

这篇关于从浏览器发送 websocket ping/pong 帧的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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