You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

SocketIO Server端保留事件咨询:官方文档未提及但示例可见

Socket.IO Server-Side Reserved Events: A Clear Breakdown

Great question! I totally get why this is confusing—Socket.IO's server-side reserved events aren't always compiled into a single easy-to-find list in the official docs, even though they're used constantly in examples like the connection event you referenced:

io.on('connection', function(socket) {.....}

Here’s a comprehensive list of the key server-side reserved events, along with what they do:

  • connection (emitted on the main io instance): Triggers when a client successfully establishes a connection to your server. The callback receives the socket object that represents the individual client connection—this is where you’ll set up listeners for custom events, join rooms, and handle client-specific logic.
  • disconnect (emitted on individual socket instances): Fires when a client’s connection drops (whether intentionally or due to an error). Use this to clean up resources, update user statuses, or log offline events.
  • disconnecting (emitted on individual socket instances): A pre-disconnect event that triggers before the client is fully disconnected. This is useful if you need to perform actions while the client is still technically connected (like removing them from rooms or saving session data).
  • error (emitted on individual socket instances): Catches connection-related errors specific to a client (e.g., network issues, invalid payloads). The callback receives the error object for debugging.
  • connect_error (emitted on the main io instance): Triggers when a client attempts to connect but fails (e.g., invalid authentication, server overload). Use this to log connection failures or handle retry logic.

The reason these aren’t always easy to spot in the official docs is that they’re often covered in context rather than a dedicated "reserved events" section. For example, connection is highlighted in the basic usage guide, while others are mentioned in sections on error handling or connection lifecycle.

内容的提问来源于stack exchange,提问作者Hairi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 06:26:43