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

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

问题描述

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



我问的原因是我使用的是在Mongrel2后面运行的python框架,所以我想知道是否有一种向Mongrel2发送特定ping / pong消息的方法,该消息将告诉它保持连接活着,而我的python应用程序不需要担心它。我想,类似于有一个单独的HTTP方法。而且我认为专用的ping / pong消息帧可能比字符串ping更简单(服务器和网络上的负载更少),但这可能不会太重要。



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

解决方案

没有Javascript API发送ping帧或接收pong帧。这是您的浏览器支持或不支持。也没有API来启用,配置或检测浏览器是否支持并使用ping / pong帧。有关于为此创建Javascript ping / pong API的讨论 。 pings有可能在未来可配置/可检测,但Javascript不太可能直接发送和接收ping / pong帧。



但是,如果您同时控制客户端和服务器代码,那么您可以轻松地在更高级别添加ping / pong支持。如果您还没有,那么您的消息中将需要某种消息类型标题/元数据,但这非常简单。除非你计划每秒发送数百次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?

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.

EDIT: 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?

解决方案

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.

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天全站免登陆