Skip to main content
Version: 4.x

处理断开连接

¥Handling disconnections

现在,让我们重点介绍 Socket.IO 的两个非常重要的属性:

¥Now, let's highlight two really important properties of Socket.IO:

  1. Socket.IO 客户端并不总是处于连接状态

    ¥a Socket.IO client is not always connected

  2. Socket.IO 服务器不存储任何事件

    ¥a Socket.IO server does not store any event

caution

即使在稳定的网络上,也不可能永远保持连接。

¥Even over a stable network, it is not possible to maintain a connection alive forever.

这意味着你的应用需要能够在暂时断开连接后将客户端的本地状态与服务器上的全局状态同步。

¥Which means that your application needs to be able to synchronize the local state of the client with the global state on the server after a temporary disconnection.

note

Socket.IO 客户端将在短暂延迟后自动尝试重新连接。然而,对于该客户端来说,在断开连接期间任何遗漏的事件都将实际上丢失。

¥The Socket.IO client will automatically try to reconnect after a small delay. However, any missed event during the disconnection period will effectively be lost for this client.

在我们的聊天应用的上下文中,这意味着断开连接的客户端可能会遗漏一些消息:

¥In the context of our chat application, this implies that a disconnected client might miss some messages:

The disconnected client does not receive the 'chat message' eventThe disconnected client does not receive the 'chat message' event

我们将在接下来的步骤中了解如何改进这一点。

¥We will see in the next steps how we can improve this.