阻止 socket.io 重新连接 [英] Stop socket.io from reconnecting
本文介绍了阻止 socket.io 重新连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
简单场景:
- 客户端使用 socket.io 连接到服务器 (
socket = io.connect(...)
) - 服务器崩溃
- 客户端尝试重新连接
- 用户告诉客户端断开连接 (
socket.disconnect()
) - 服务器启动
- 客户端重新连接
似乎一旦 socket.io 开始尝试重新连接,它就无法再停止了.我想要第 4 步来防止第 6 步发生,但事实并非如此.我该怎么称呼?
It seems that once socket.io starts attempting to reconnect, it cannot be stopped anymore. I want step 4 to prevent step 6 from happening, but it doesn't. What do I have to call instead?
推荐答案
您可能想要自己处理重新连接.
You might want to handle the reconnection yourself.
// Disables the automatic reconnection
var socket = io.connect('http://server.com', {
reconnection: false
});
// Reconnects on disconnection
socket.on('disconnect', function(){
socket.connect(callback);
});
注意:旧版本使用 reconnect
而不是 reconnection
.
Note: old versions used reconnect
instead of reconnection
.
这篇关于阻止 socket.io 重新连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文