【问题标题】:How can I detect, when the connection of the client gets interrupted in socket.io?当客户端的连接在 socket.io 中中断时,如何检测?
【发布时间】:2011-11-07 00:27:19
【问题描述】:

我有一个带有 socket.io 连接到浏览器客户端的 Node.js-Server。有时连接会中断,例如,当我需要重新启动服务器时。发生这种情况时,客户怎么会知道呢?

【问题讨论】:

    标签: node.js socket.io


    【解决方案1】:

    这是一个关于如何在客户端实现这一目标的示例:

    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)

    【讨论】:

    • 谢谢,比我想的更容易:)
    猜你喜欢
    • 2012-05-17
    • 2015-11-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-17
    • 2020-08-27
    • 2014-01-24
    相关资源
    最近更新 更多