HTML5网络套接字 - 如何与它们通信? [英] HTML5 web sockets - how to communicate with them?

查看:121
本文介绍了HTML5网络套接字 - 如何与它们通信?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经测试了很多套接字示例,并且它们之间的通信非常简单:套接字打开并保持打开状态,直到您关闭它并且除了您发送的信息之外没有信息被发送。

I've tested many socket examples and communication between them was pretty simple: socket is opened and stays open until you close it and no information is being sent except the one you have sent.

对于HTML5网络套接字,这两个时刻是不同的。

With HTML5 web sockets these two moments are different.

首先,一旦客户端HTML5套接字连接到服务器套接字,它就会发送一堆信息:

At first, as soon as the client HTML5 socket connects to server socket it sends a bunch of information:

GET /echo HTTP/1.1
Upgrade: websocket
Connection: Upgrade
Host: localhost:2002
Origin: null
Pragma: no-cache
Cache-Control: no-cache
Sec-WebSocket-Key: iYzsmhdzg6h6/UGtCLLGVA==
Sec-WebSocket-Version: 13
Sec-WebSocket-Extensions: permessage-deflate; client_max_window_bits, x-webkit-deflate-frame
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.114 Safari/537.36
Cookie: _rails-socket-listener_session=dGtleFYxNjhIaUZrZVpOWUNIMHdFZFd6WW9wY2FJYjIwOWdSMFVPR1ZkYkZUakExdVlhNzMvWEphNG1IRUIvT1JsQ0N6bHF4REFXTkJUemE4R2RjOER6bXdhSEt6M0tIYmRwV0w3VzkrVGt4MzN2Z0M3MXMyYndZR3hvOGMySTJTZmdEMW9JdEE5ZERuSDB4VCtROFNnPT0tLTlheG1KamlBSVVmT0tUZ1F5bmQ0OUE9PQ%3D%3D--23413749a30295f08d277292837c76187a02a332

如何解释这个信息?该怎么办?

How to interpret this information? What to do with it?

第二,当我从调试服务器(Hercules安装实用程序)发送一些字符串时,socket的 onmessage 事件未被触发,客户端套接字在此之后立即关闭连接。

At second, when I send some string from debugging server (Hercules setup utility), socket's onmessage event is not fired and client socket closes the connection immediately after this.

因此,我认为HTML5 Web套接字在使用之前需要进行一些握手。在哪里阅读它?

So, I suppose that HTML5 web socket expects some handshaking before it could be used. Where to read about it?

BTW:我使用Ruby作为服务器端语言。

BTW: I am using Ruby as server-side language.

推荐答案


我测试过许多网络套接字示例....

I've tested many web socket examples ....

根据您的描述,您没有使用网络套接字,而只使用套接字,例如直接TCP / IP。 WebSockets(例如您所谓的HTML Web套接字)是不同的:它们用于通过已建立的HTTP连接创建套接字。因此,您会看到带有Upgrade:websocket标题的HTTP查询,带有Sec-WebSocket-Key等。

From what you describe you did not use "web sockets" but simply "sockets", e.g. direct TCP/IP. WebSockets (e.g. what you call "HTML web sockets") are different: they are used to create something socket like over an established HTTP connection. Therefore you see the HTTP query with the "Upgrade: websocket" header, with the "Sec-WebSocket-Key" etc.

建立连接后(服务器发送响应代码101)WebSocket连接有自己的数据框架和加扰,因此您不能只使用普通的套接字工具。

After establishing the connection (server sends response with code 101) the WebSocket connection has their own framing and scrambling of the data, so you cannot just use it with the normal socket tools.

请参阅 http://tools.ietf.org / html / rfc6455 用于规范,如果您查找更多文档,用法,请使用WebSockets作为关键字...

See http://tools.ietf.org/html/rfc6455 for the specification and use "WebSockets" as the keyword if you look for more documentation, usage...

这篇关于HTML5网络套接字 - 如何与它们通信?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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