【发布时间】:2011-11-07 00:27:19
【问题描述】:
我有一个带有 socket.io 连接到浏览器客户端的 Node.js-Server。有时连接会中断,例如,当我需要重新启动服务器时。发生这种情况时,客户怎么会知道呢?
【问题讨论】:
我有一个带有 socket.io 连接到浏览器客户端的 Node.js-Server。有时连接会中断,例如,当我需要重新启动服务器时。发生这种情况时,客户怎么会知道呢?
【问题讨论】:
这是一个关于如何在客户端实现这一目标的示例:
var chat = io.connect('http://localhost:4000/chat');
chat.on('connect', function () {
console.log('Connected to the chat!');
});
chat.on('disconnect', function () {
console.log('Disconnected from the chat!');
});
如您所见,您保留连接变量并使用 connection_variable.on('disconnect', callback_function_here)
【讨论】: