socket.io如何工作? [英] how does socket.io work?

查看:159
本文介绍了socket.io如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用socket.io并且设置很快(感谢其使用页面上的示例),但我想了解更多关于封面下究竟发生了什么以及使其工作的技术是什么。

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?

它是在80端口还是单独的端口?

is it on port 80 or a separate one?

它真的保持开放还是被模拟的部分?

does it really stay open or is that part simulated?

有没有办法分析每个套接字事件?
(有点像使用fiddler查看ajax调用中发生的事情)

is there a way to profile each socket event ? (sort of like using fiddler to see what happens in ajax calls)

推荐答案

对于调试,你可能想要试用 Theseus

For debugging, you might want to try out Theseus.

以下是 socket.io SPEC 的简短概述:

Here is a short overview of the socket.io SPEC:


Socket.IO旨在为许多浏览器和
设备带来类似WebSocket的API,其中包含一些特定功能,可帮助创建
实际应用和游戏。

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.


  • 多种传输支持(旧用户代理,移动浏览器等)。

  • 同一连接下的多个套接字(名称空间)。

  • 通过检测信号断开检测。

  • 可选的acknoledments。

  • 带缓冲的重新连接支持(适用于移动设备或坏网络)

  • 位于HTTP之上的轻量级协议。

  • 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.

Socket.IO客户端首先决定要用于连接的传输。

A Socket.IO client first decides on a transport to utilize to connect.

Socket.IO套接字的状态可以是已断开连接
断开连接已连接连接

The state of the Socket.IO socket can be disconnected, disconnecting, connected and connecting.

传输连接可以已关闭关闭打开
打开

简单的HTTP握手发生在Socket.IO
连接的开头。握手,如果成功,将导致客户端
收到:

A simple HTTP handshake takes place at the beginning of a Socket.IO connection. The handshake, if successful, results in the client receiving:


  • 将为运输打开的会话ID连接。

  • 预期心跳的秒数(心跳超时

  • 传输连接关闭后的几秒钟,如果传输连接未重新打开(关闭超时),则认为套接字已断开连接。

  • 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.

如果您需要更多详细信息,您可以阅读此处的其他规范

If you need more of the details, you can read the rest of the specification here

这篇关于socket.io如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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