javascript socket.io 是如何工作的?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16719282/
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
how does socket.io work?
提问by Sonic Soul
I am using socket.io and it was quick to setup (thanks to examples on their usage page) but i'd like to find out more about what exactly is going on under covers and what's the technology that makes it work.
我正在使用 socket.io 并且设置很快(感谢他们使用页面上的示例),但我想了解更多关于幕后到底发生了什么以及使其工作的技术是什么。
what are the exact mechanics of socket.io?
socket.io 的确切机制是什么?
is it on port 80 or a separate one?
它是在端口 80 上还是一个单独的端口?
does it really stay open or is that part simulated?
它真的保持打开状态还是那部分是模拟的?
is there a way to profile each socket event ? (sort of like using fiddler to see what happens in ajax calls)
有没有办法分析每个套接字事件?(有点像使用 fiddler 来查看 ajax 调用中会发生什么)
采纳答案by JAM
For debugging, you might want to try out Theseus.
对于调试,您可能想尝试使用忒修斯。
Here is a short overview of the socket.io SPEC:
以下是socket.io SPEC的简短概述:
Socket.IO aims to bring a WebSocket-like API to many browsers and devices, with some specific features to help with the creation of real-world realtime applications and games.
- Multiple transport support (old user agents, mobile browsers, etc).
- Multiple sockets under the same connection (namespaces).
- Disconnection detection through heartbeats.
- Optional acknoledgments.
- Reconnection support with buffering (ideal for mobile devices or bad networks)
- Lightweight protocol that sits on top of HTTP.
Anatomy of a Socket.IO socket
A Socket.IO client first decides on a transport to utilize to connect.
The state of the Socket.IO socket can be
disconnected
,disconnecting
,connected
andconnecting
.The transport connection can be
closed
,closing
,open
, andopening
.A simple HTTP handshake takes place at the beginning of a Socket.IO connection. The handshake, if successful, results in the client receiving:
- A session id that will be given for the transport to open connections.
- A number of seconds within which a heartbeat is expected (
heartbeat timeout
)- A number of seconds after the transport connection is closed when the socket is considered disconnected if the transport connection is not reopened (
close timeout
).At this point the socket is considered connected, and the transport is signaled to open the connection.
If the transport connection is closed, both ends are to buffer messages and then frame them appropriately for them to be sent as a batch when the connection resumes.
If the connection is not resumed within the negotiated timeout the socket is considered disconnected. At this point the client might decide to reconnect the socket, which implies a new handshake.
Socket.IO 旨在为许多浏览器和设备带来类似 WebSocket 的 API,并具有一些特定功能来帮助创建真实世界的实时应用程序和游戏。
- 多种传输支持(旧用户代理、移动浏览器等)。
- 同一连接(命名空间)下的多个套接字。
- 通过心跳检测断开连接。
- 可选的确认。
- 带缓冲的重新连接支持(非常适合移动设备或不良网络)
- 位于 HTTP 之上的轻量级协议。
Socket.IO 套接字剖析
Socket.IO 客户端首先决定用于连接的传输。
在Socket.IO插座的状态可以是
disconnected
,disconnecting
,connected
和connecting
。传输连接可以是
closed
,closing
,open
,和opening
。一个简单的 HTTP 握手发生在 Socket.IO 连接的开始。如果握手成功,客户端会收到:
- 将为传输提供的会话 ID 以打开连接。
- 预期心跳的秒数 (
heartbeat timeout
)- 如果未重新打开传输连接,则认为套接字已断开时,传输连接关闭后的秒数 (
close timeout
)。此时套接字被认为已连接,并且传输信号以打开连接。
如果传输连接关闭,则两端都将缓冲消息,然后适当地对它们进行帧处理,以便在连接恢复时将它们作为批处理发送。
如果在协商的超时时间内没有恢复连接,则认为套接字已断开连接。此时客户端可能决定重新连接套接字,这意味着新的握手。
If you need more of the details, you can read the rest of the specification here
如果您需要更多详细信息,可以在此处阅读规范的其余部分
回答by josh3736
JAM's post does a good job of summarizing what socket.io is; I'd like to specifically address some of your other questions.
JAM 的帖子很好地总结了 socket.io是什么;我想专门解决您的其他一些问题。
Socket.io attaches to an instance of
http.Server
and adds handlers to it. It does not listen to a network port on its own; it simply adds socket.io-specific handlers to an existing HTTP server. (However, if you callio.listen()
with a number, it internally creates a new HTTP server that listens to the specified port and attaches to that.)It really stays open if it is using the WebSocketstransport. It also includes fallback mechanisims that use traditional (long-)polling ajax requests. So the answer depends on what APIs the browser supports. (You can optionally configure which fallbacks you'd like to use, if any.)
Fiddler supports websockets now, as does Chrome's developer tools:
Socket.io 附加到 的实例
http.Server
并向其添加处理程序。它不会自行侦听网络端口;它只是将 socket.io 特定的处理程序添加到现有的 HTTP 服务器。(但是,如果您io.listen()
使用号码调用,它会在内部创建一个新的 HTTP 服务器,该服务器侦听指定端口并附加到该端口。)如果它使用WebSockets传输,它确实保持打开状态。它还包括使用传统(长)轮询 ajax 请求的回退机制。所以答案取决于浏览器支持哪些 API。(您可以选择配置要使用的回退,如果有的话。)
Fiddler 现在支持 websockets,Chrome 的开发者工具也支持: