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

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

问题描述

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

socket.io 的确切机制是什么?

它是在端口 80 上还是在一个单独的端口上?

它真的保持打开状态还是那部分是模拟的?

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

解决方案

对于调试,您可能需要尝试忒修斯.

以下是 socket.io SPEC 的简短概述::><块引用>

Socket.IO 旨在为许多浏览器带来类似 WebSocket 的 API,并且设备,具有一些特定的功能,以帮助创建现实世界的实时应用程序和游戏.

  • 多种传输支持(旧用户代理、移动浏览器等).
  • 同一连接下的多个套接字(命名空间).
  • 通过心跳检测断开连接.
  • 可选的确认.
  • 带缓冲的重新连接支持(非常适合移动设备或不良网络)
  • 位于 HTTP 之上的轻量级协议.

Socket.IO 套接字剖析

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

Socket.IO socket的状态可以disconnected正在断开已连接正在连接.

传输连接可以是closedclosureopen开场.

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

  • 将为传输提供的会话 ID,用于打开连接.
  • 预期心跳的秒数(心跳超时)
  • 如果传输连接被认为已断开,传输连接关闭后的秒数未重新打开(关闭超时).

此时socket被认为是连接的,传输是发出打开连接的信号.

如果传输连接关闭,两端都要缓冲消息,然后适当地将它们框起来,以便将它们作为连接恢复时批处理.

如果在协商的超时时间内没有恢复连接套接字被认为是断开的.此时客户端可能决定重新连接套接字,这意味着新的握手.

如果您需要更多详细信息,可以在此处

阅读规范的其余部分

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.

what are the exact mechanics of socket.io?

is it on port 80 or a separate one?

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)

解决方案

For debugging, you might want to try out Theseus.

Here is a short overview of the 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 and connecting.

The transport connection can be closed, closing, open, and opening.

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.

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

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

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