常见问题
以下是有关 Socket.IO 的常见问题列表:
¥Here is a list of common questions about Socket.IO:
有些东西不能正常工作,请帮忙?
¥Something does not work properly, please help?
请检查 故障排除指南。
¥Please check the Troubleshooting guide.
它在幕后是如何工作的?
¥How does it work under the hood?
Socket.IO 连接可以通过不同的底层传输建立:
¥The Socket.IO connection can be established with different low-level transports:
HTTP 长轮询
¥HTTP long-polling
Socket.IO 将自动选择最佳可用选项,具体取决于:
¥Socket.IO will automatically pick the best available option, depending on:
网络(某些网络阻止 WebSocket 和/或 WebTransport 连接)
¥the network (some networks block WebSocket and/or WebTransport connections)
你可以在 "工作原理" 节 中找到更多详细信息。
¥You can find more detail about that in the "How it works" section.
Socket.IO 相对于普通 WebSocket 提供了哪些功能?
¥What are the features provided by Socket.IO over plain WebSocket?
WebSocket 太棒了!不完全是。它们提供了一种在客户端和服务器之间传输数据的有效方法。优点包括:
¥WebSockets are awesome! No, really. They provide an efficient way for transferring data between a client and a server. Among the advantages:
你不需要依赖定期轮询从服务器获取数据
¥you don't need to rely on periodic polling to fetch data from the server
向服务器发送数据时无需重复发送所有 HTTP 标头
¥you don't need to repeatedly send all the HTTP headers when sending data to the server
这使得它们非常适合低延迟和数据密集型应用,例如游戏、聊天、协作解决方案......
¥Which make them perfect for low-latency and data-intensive applications like games, chats, collaborative solutions...
话虽这么说,WebSocket 也是相当底层的,使用 WebSocket 开发实时应用通常需要在它们之上添加一个附加层:
¥That being said, WebSockets are also pretty low-level and developing a realtime applications with WebSockets often requires an additional layer over them:
回退到 HTTP 长轮询,以防无法建立 WebSocket 连接
¥fallback to HTTP long-polling, in case the WebSocket connection can't be established
自动重新连接,以防 WebSocket 连接关闭
¥automatic reconnection, in case the WebSocket connection gets closed
确认,发送一些数据并期待对方的响应
¥acknowledgements, to send some data and expect a response from the other side
向所有或部分连接的客户端广播
¥broadcast to all or to a subset of connected clients
扩展到服务器的多个实例
¥scale up to multiple instances of the server
连接恢复,用于短时间的断开连接
¥connection recovery, for short periods of disconnection
正如你可能已经猜到的,这个附加层是由 Socket.IO 库实现的。
¥As you might have guessed, this additional layer is implemented by the Socket.IO library.
什么是 WebTransport?
¥What is WebTransport?
简而言之,WebTransport 是 WebSocket 的替代方案,它修复了困扰 队头阻塞 等 WebSocket 的几个性能问题。
¥In short, WebTransport is an alternative to WebSocket which fixes several performance issues that plague WebSockets like head-of-line blocking.
如果你想了解有关此新 Web API(于 2022 年 1 月包含在 Chrome 中,并于 2023 年 6 月包含在 Firefox 中)的更多信息,请检查以下链接:
¥If you want more information about this new web API (which was included in Chrome in January 2022 and in Firefox in June 2023), please check those links:
默认情况下,Socket.IO 中未启用对 WebTransport 的支持,因为它需要安全上下文 (HTTPS)。如果你想使用 WebTransport,请检查 专用教程。
¥Support for WebTransport is not enabled by default in Socket.IO, as it requires a secure context (HTTPS). Please check the dedicated tutorial if you want to play with WebTransport.
Socket.IO 是否存储消息?
¥Does Socket.IO store the messages?
Socket.IO 服务器不存储任何消息。
¥The Socket.IO server does not store any message.
你的应用有责任为当前未连接的客户端保留这些消息。
¥It is the duty of your application to persist those messages somewhere for the clients that are not currently connected.
话虽这么说,如果你启用 连接状态恢复功能,Socket.IO 会将消息存储一小段时间。
¥That being said, Socket.IO will store the messages for a brief period of time if you enable the Connection state recovery feature.
Socket.IO 的交付保证是什么?
¥What are the delivery guarantees of Socket.IO?
Socket.IO 确实保证消息排序,无论使用哪种底层传输(即使在两种传输之间切换时)。
¥Socket.IO does guarantee message ordering, no matter which low-level transport is used (even when switching between two transports).
此外,默认情况下,Socket.IO 提供最多一次传送保证(也称为 "即发即忘"),这意味着在某些情况下消息可能会丢失并且不会尝试重试。
¥Moreover, by default Socket.IO provides an at most once guarantee of delivery (also known as "fire and forget"), which means that under certain circumstances a message might get lost and no retry will be attempted.
有关此 此处 的更多信息。
¥More information about this here.
如何识别给定用户?
¥How to identify a given user?
Socket.IO 中没有用户的概念。
¥There is no concept of user in Socket.IO.
你的应用有责任将给定的 Socket.IO 连接链接到用户账户。
¥It is the duty of your application to link a given Socket.IO connection to a user account.
对于 Node.js 应用,你可以:
¥For Node.js applications, you can for example:
¥reuse the user context provided by Passport (check this tutorial)
或者在客户端使用
auth
选项发送用户凭据并在 中间件 中验证它们¥or use the
auth
option on the client side to send the user credentials and validate them in a middleware
我在哪里可以找到变更日志?
¥Where can I find the changelog?
请参阅 此处。
¥Please see here.