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 mainioinstance): Triggers when a client successfully establishes a connection to your server. The callback receives thesocketobject 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 individualsocketinstances): 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 individualsocketinstances): 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 individualsocketinstances): 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 mainioinstance): 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
相关产品推荐
相关产品推荐

