JavaScript 和 WebSockets:使用特定协议
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7363095/
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
JavaScript and WebSockets: using specific protocol
提问by KorHosik
I'm currently working with WebSockets and a PHP server: it works very well with Google Chrome and Opera, but not with Firefox 6.
我目前正在使用 WebSockets 和 PHP 服务器:它适用于 Google Chrome 和 Opera,但不适用于 Firefox 6。
I think it's due to the protocol version this last uses: I see somewhere that it uses the seventh version, whereas it's an older one for Google Chrome and Opera.
我认为这是由于最后使用的协议版本:我在某处看到它使用第七个版本,而对于 Google Chrome 和 Opera 来说它是旧版本。
So, I modified my server code in order to manage this new version: by hashing the secure-key with 258EAFA5-E914-47DA-95CA-C5AB0DC85B11and other stuffs, Firefox succeeds to connect. But if another client wants to connect (even another Firefox), the first one with Firefox deconnects itself.
因此,我修改了我的服务器代码以管理这个新版本:通过使用258EAFA5-E914-47DA-95CA-C5AB0DC85B11和其他东西散列安全密钥,Firefox 成功连接。但是如果另一个客户端想要连接(甚至是另一个 Firefox),第一个使用 Firefox 的客户端会自动断开连接。
I saw that buffer received by socket_recv() is either empty or hashed...
我看到 socket_recv() 接收的缓冲区是空的或散列的......
So I decided to skip the idea of managing the protocol used by Firefox 6 (there are no documentation on Internet... !): I think it could be easier to specify the protocol to use directly in JavaScript.
所以我决定跳过管理 Firefox 6 使用的协议的想法(互联网上没有文档......!):我认为指定协议以直接在 JavaScript 中使用会更容易。
On this pagethey say that we can write this:
在这个页面上,他们说我们可以这样写:
var mySocket = new WebSocket("http://www.example.com/socketserver", "my-custom-protocol");
But what should we write instead of "my-custom-protocol"in order to use the protocol managed by Google Chrome and Opera?
但是为了使用 Google Chrome 和 Opera 管理的协议,我们应该写什么来代替“my-custom-protocol”?
Thanks in advance!
提前致谢!
回答by kanaka
The protocol option to the WebSocket constructor is really a "sub-protocol" (it is often called by that name) and it is an application level sub-protocol. It does not have any effect on the actual WebSocket protocol version. Browsers basically support a single version of the WebSocket protocol itself. Most servers support several versions of the protocol.
WebSocket 构造函数的协议选项实际上是一个“子协议”(它通常被称为该名称)并且它是一个应用程序级子协议。它对实际的 WebSocket 协议版本没有任何影响。浏览器基本上支持 WebSocket 协议本身的单一版本。大多数服务器支持多个版本的协议。
Firefox 6.0 introduced support for the new HyBi series of protocols (HyBi-00 is really just a copy of the Hixie-76 protocol). The HyBi versions introduce a new framing format for data and are not just a change to the handshake. Chrome 14 also uses the new HyBi protocol series.
Firefox 6.0 引入了对新的 HyBi 系列协议的支持(HyBi-00 实际上只是 Hixie-76 协议的副本)。HyBi 版本引入了一种新的数据帧格式,而不仅仅是对握手的更改。Chrome 14 还使用了新的 HyBi 协议系列。
Here is the most recent version of the WebSockets protocol: http://tools.ietf.org/html/draft-ietf-hybi-thewebsocketprotocol-14although firefox 6.0 is actually this one http://tools.ietf.org/html/draft-ietf-hybi-thewebsocketprotocol-07but there aren't really that many real changes (mostly textual changes to the spec itself).
这是 WebSockets 协议的最新版本:http: //tools.ietf.org/html/draft-ietf-hybi-thewebsocketprotocol-14虽然 firefox 6.0 实际上是这个http://tools.ietf.org/html /draft-ietf-hybi-thewebsocketprotocol-07但实际上并没有那么多真正的变化(主要是规范本身的文本变化)。
Are you certain that firefox is connecting successfully (i.e. do you actually get an onopen event in the browser)?
您确定 firefox 连接成功(即您是否真的在浏览器中收到了 onopen 事件)?