Socket.io - 客户端断开后手动重新连接 [英] Socket.io - Manual reconnect after client-side disconnect

查看:6107
本文介绍了Socket.io - 客户端断开后手动重新连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 node.js socket.io 来创建实时Web应用程序。我将给用户完全控制套接字连接,如手动断开连接和(重新)连接。

I use node.js and socket.io to create a real time web application. I will give the users full control of the socket connection, like manual disconnect and (re)connect.

function socket_connect()
{
    console.log('func socket_connect');
    socket = io.connect('http://url/to/the/app');
}

function socket_reconnect()
{
    console.log('func socket_reconnect');
    socket_connect();
}

function socket_disconnect ()
{
    console.log('func socket_disconnect');
    if (socket) socket.disconnect();
}

在客户端启动 socket_connect()函数工作正常,但在使用 socket.disconnect()后,没有新的连接开始。

On client start up the socket_connect() function works fine, but after using socket.disconnect(), no new connection starts.

推荐答案

现在可以使用socket.socket.reconnect()

It works now, with socket.socket.reconnect()

function socket_connect()
{
    console.log('func socket_connect');
    socket = io.connect('http://url/to/the/app');
}

function socket_reconnect()
{
    console.log('func socket_reconnect');
    socket.socket.reconnect();
}

function socket_disconnect ()
{
    console.log('func socket_disconnect');
    if (socket) socket.disconnect();
}

相关: https://github.com/LearnBoost/socket.io-client/issues/251

这篇关于Socket.io - 客户端断开后手动重新连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆