从 socket.io 迁移到原始 websockets? [英] Moving from socket.io to raw websockets?

查看:40
本文介绍了从 socket.io 迁移到原始 websockets?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在我使用 socket.io 和强制 websockets 作为传输.我正在考虑迁移到原始 websockets,但我不清楚从 socket.io 迁移会丢失哪些功能.感谢您的任何指导.

Right now I'm using socket.io with mandatory websockets as the transport. I'm thinking about moving to raw websockets but I'm not clear on what functionality I will lose moving off of socket.io. Thanks for any guidance.

推荐答案

socket.io 库在标准 webSockets 之外添加了以下功能:

The socket.io library adds the following features beyond standard webSockets:

  1. 如果浏览器不支持 webSockets 或者如果网络路径具有阻止 webSockets 的代理/防火墙,则自动选择长轮询与 webSocket.

  1. Automatic selection of long polling vs. webSocket if the browser does not support webSockets or if the network path has a proxy/firewall that blocks webSockets.

如果连接中断(即使服务器重新启动),客户端会自动重新连接.

Automatic client reconnection if the connection goes down (even if the server restarts).

自动检测死连接(通过使用常规 ping 检测无效连接)

Automatic detection of a dead connection (by using regular pings to detect a non-functioning connection)

自动转换为 JSON 的消息传递方案.

Message passing scheme with automatic conversion to/from JSON.

服务器端房间的概念,可以轻松地与一组连接的用户进行通信.

The server-side concept of rooms where it's easy to communicate with a group of connected users.

连接到服务器上的命名空间而不仅仅是连接到服务器的概念.这可以用于各种不同的功能,但我用它来告诉服务器我想订阅什么类型的信息.这就像连接到特定频道.

The notion of connecting to a namespace on the server rather than just connecting to the server. This can be used for a variety of different capabilities, but I use it to tell the server what types of information I want to subscribe to. It's like connection to a particular channel.

服务器端数据结构,自动跟踪所有连接的客户端,以便您可以随时枚举它们.

Server-side data structures that automatically keep track of all connected clients so you can enumerate them at any time.

socket.io 库中内置的中间件架构,可用于实现诸如通过访问原始连接中的 cookie 进行身份验证之类的事情.

Middleware architecture built-in to the socket.io library that can be used to implement things like authentication with access to cookies from the original connection.

在首次连接时自动存储连接上存在的 cookie 和其他标头(对于识别连接的用户非常有用).

Automatic storage of the cookies and other headers present on the connection when it was first connected (very useful for identifying what user is connected).

服务器端广播功能,可向所有连接的客户端、房间中的所有客户端或命名空间中的所有客户端发送通用消息.

Server-side broadcast capabilities to send a common message to either to all connected clients, all clients in a room or all clients in a namespace.

使用消息名称标记每条消息,并将消息名称路由到 eventEmitter 中,这样您就可以通过侦听 eventEmitter 以获取所需消息名称来侦听传入消息.

Tagging of every message with a message name and routing of message names into an eventEmitter so you listen for incoming messages by listening on an eventEmitter for the desired message name.

这篇关于从 socket.io 迁移到原始 websockets?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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