问题描述
我想知道socket.io方法是如何发出特定事件的,我读到它不像长轮询方法,而是一种可以在所有不同浏览器上工作的方法……怎么可能客户端与服务器保持联系而无需长时间的轮询请求?
I would like to know how socket.io methods work for emitting a certain event, I've read that it is not like long-polling method but a different one that can work on all of different browsers ... How could a client keep-in-contact with the server without a long polling request?
我是node.js的新手,我想为事件驱动的服务器实现我自己的系统(是的...重新发明轮子!),因为我想亲手触摸一下这个如此简单的套接字所发生的事情. io-generate()方法.
I'm new at node.js and I would like to implement my own system for eventdriven servers (yes...reinvent the wheel!) because I want to touch by hands what is going behind this so easy "socket.io - emit()" method.
感谢您的帮助!
推荐答案
socket.io的工作原理如下:
Here's how socket.io works in a nutshell:
首先,socket.io是webSocket之上的一个薄协议层,因此,实际上,我们将讨论webSocket的工作原理.
First off, socket.io is a thin protocol layer on top of a webSocket, so really, we're going to discuss how a webSocket works.
-
webSocket连接从具有一个特殊标头集的普通HTTP请求开始,Upgrade标头请求从HTTP协议升级到webSocket协议.
A webSocket connection starts with an ordinary HTTP request with one special header set, the Upgrade header which requests an upgrade from the HTTP protocol to the webSocket protocol.
当服务器接收到请求并看到升级标头时,如果它支持该协议,则它将以101响应(交换协议)和一些其他标头进行响应.当客户端收到此响应(以及一些其他与安全性相关的标头)时,两端将该套接字上的协议切换为webSocket协议.
When the server receives the request and sees the upgrade header, if it supports that protocol, then it responds with a 101 response (switching protocols) and some other headers. When the client receives this response (and some other headers related to security), then both ends switch protocol on that very socket to the webSocket protocol.
因为webSocket协议旨在持续连接客户端和服务器,因此保持原始套接字开放以供将来进行通信,实际上,它保持开放状态,直到双方决定最终通过webSocket通信通道完成然后关闭套接字.
Because the webSocket protocol is designed to be continuously connected both client and server keep the original socket open for future communication and, in fact, it stays open until either side decides that they are eventually done with the webSocket communication channel and then closes the socket.
因此,插座长时间保持打开状态.这样一来,任何一方都可以随时向另一方发送消息.这样可以避免轮询.客户端可以只是坐在那里侦听传入的消息,而不必从客户端请求服务器的临时HTTP请求:您是否有m的任何新数据?".当服务器有一些新内容要发送给客户端时,它已经具有打开的webSocket连接,并且可以随时向客户端发送消息.
So, the socket stays open for a long time. This enables either side to send a message to the other at any time. This is how it avoids polling. Rather than a temporal HTTP request from the client that asks the server: "Do you have any new data for m?", the client can just sit there and listen for incoming messages. When the server has something new to send the client, it already has this open webSocket connection and it can just send a message to the client at any time.
Socket.io在webSocket之上添加了一些功能.它添加的主要内容是:1)如果套接字连接由于某种原因而丢失,则自动重新连接; 2)从一端到另一端进行定期ping,以检测何时丢失了连接;以及3)使它变得微不足道的消息传递层.从一端向另一端发送命名消息.
Socket.io addds a few features on top of a webSocket. The main things it adds are: 1) Auto-reconnection if the socket connection is lost for any reason, 2) Regular pings from one end to the other to detect when the connection is lost and 3) A messaging layer that makes it trivial to send named messages from one end to the other.
这不是很长时间的轮询,因为它在客户端期间保持套接字连接打开.此长期开放的连接可用于仅从客户端到服务器或从服务器到客户端发送消息,而无需每次要发送时都创建新的连接.在任何支持webSocket协议的浏览器中都可以使用.
It's not long polling because it keeps a socket connection open for the duration of the client. This long-lived, open connection can be used to just send messages from either client to server or from server to client without having to create a new connection each time you want to send. This will work in any browser that has support for the webSocket protocol.
webSocket连接被设计为长期连接,而不是典型的临时HTTP连接.
The webSocket connection is designed to be long-lived connection rather the typical temporal HTTP connection.
如果您想了解有关WebSocket协议本身的更多信息,可以在此MDN文章中看到一个很好的描述:编写WebSocket服务器.
If you want to read more about the webSocket protocol itself, you can see a pretty good description here in this MDN article: Writing webSocket Servers.
下面是打开webSocket的初始客户端请求的示例:
Here's an example of an initial client request to open a webSocket:
GET /chat HTTP/1.1
Host: example.com:8000
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==
Sec-WebSocket-Version: 13
这是一个典型的服务器响应,该响应确认已切换到webSocket协议:
And, here's a typical server response that acknowledges the switch to the webSocket protocol:
HTTP/1.1 101 Switching Protocols
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=
然后,更改协议后,此处的webSocket数据框架如下所示:
Then, once the protocol has been changed, here what the webSocket data frame looks like:
0 1 2 3
0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7
+-+-+-+-+-------+-+-------------+-------------------------------+
|F|R|R|R| opcode|M| Payload len | Extended payload length |
|I|S|S|S| (4) |A| (7) | (16/64) |
|N|V|V|V| |S| | (if payload len==126/127) |
| |1|2|3| |K| | |
+-+-+-+-+-------+-+-------------+ - - - - - - - - - - - - - - - +
4 5 6 7
+ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +
| Extended payload length continued, if payload len == 127 |
+ - - - - - - - - - - - - - - - +-------------------------------+
8 9 10 11
+ - - - - - - - - - - - - - - - +-------------------------------+
| |Masking-key, if MASK set to 1 |
+-------------------------------+-------------------------------+
12 13 14 15
+-------------------------------+-------------------------------+
| Masking-key (continued) | Payload Data |
+-------------------------------- - - - - - - - - - - - - - - - +
: Payload Data continued ... :
+ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +
| Payload Data continued ... |
+---------------------------------------------------------------+
Socket.io使用webSocket数据帧,但是将其自己的消息格式插入该帧的有效负载中,从而使其可以发送命名消息.
Socket.io uses the webSocket data frame, but inserts its own message format inside the payload of that frame that allows it to send named messages.
这篇关于socket.io如何工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!