简体   繁体   English

Socket.io - 客户端断开后手动重新连接

[英]Socket.io - Manual reconnect after client-side disconnect

I use node.js and socket.io to create a real time web application. 我使用node.jssocket.io来创建实时Web应用程序。 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();
}

On client start up the socket_connect() function works fine, but after using socket.disconnect() , no new connection starts. 在客户端启动时, socket_connect()函数工作正常,但在使用socket.disconnect() ,没有新连接启动。

You can reconnect by following client side config. 您可以通过以下客户端配置重新连接。

 // for socket.io version 1.0
io.connect(SERVER_IP,{'forceNew':true });

It works now, with socket.socket.reconnect() 它现在可以使用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();
}

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

If you're using Socket.io 1.0, try using the io manager on the socket to handle manual disconnection and reconnection. 如果您正在使用Socket.io 1.0,请尝试使用套接字上的io管理器来处理手动断开连接和重新连接。

// Connect to socket.io 
var socket = io.connect('url');

function manual_disconnect() {
   socket.io.disconnect();
}

function manual_reconnect() {
   socket.io.reconnect();
}

The reconnecting_attempt, reconnecting, reconnected and connected events on the socket should all be emitted afterwards. 之后应该全部发出套接字上的reconnecting_attempt,重新连接,重新连接和连接的事件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM