断开连接时Socket.io重新连接? [英] Socket.io reconnect on disconnect?

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

问题描述

这样的事情可能吗?

socket.on('disconnect', function(){
    console.log('disconnected...');
    socket.connect();
    socket.on('connect', function(){
    console.log('...reconnected');
    })  
})


推荐答案

Socket.io自动重新连接(如果你设置重新连接选项,虽然它默认为true),所以你真的不需要这样做。

Socket.io reconnects automatically (if you set the reconnect option, although it defaults to true), so you don't really need to do that.

此外,有一个重新连接事件似乎更合适。

Furthermore, there is a reconnect event which seems far more appropriate.

另外,请单独设置事件处理程序,不要在执行断开处理程序时设置连接处理程序。

Also, set your event handlers independently, don't set the connect handler in the execution of the disconnect handler.

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

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