node.js socket.io 客户端连接断开

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24566847/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 17:36:23  来源:igfitidea点击:

socket.io client connect disconnect

node.jssocket.iosocket.io-1.0

提问by Kaya Toast

I am unable to figure out why disconnecting / connecting a socket.io connection multiple times is not working ?

我无法弄清楚为什么多次断开/连接 socket.io 连接不起作用?

Server side code:

服务器端代码:

io.on('connection', function(socket){
    console.log('a user connected');
    socket.on('disconnect', function(){
        console.log('user disconnected');
    });
});

Client side code:

客户端代码:

var socket = io();
socket.on('connect', function() {
    console.log("connected from the client side");
});
$('#connect_button').click(function(){
    socket.connect();
});
$('#disconnect_button').click(function(){
    socket.disconnect();
});

It disconnects alright. But doesn't reconnect. I'm using Socket.io 1.0. Please help.

断开连接就好了。但不会重新连接。我正在使用 Socket.io 1.0。请帮忙。

回答by Jujuleder

Have you tried this config in client ?

你在客户端试过这个配置吗?

// 0.9  socket.io version
io.connect(SERVER_IP, {'force new connection': true});

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

回答by Kaya Toast

This solution, based on Jujuleder's answer works. Apparently in socket.io 1.0, it is "forceNew" instead of "force new connection" - both work though.

此解决方案基于 Jujuleder 的回答有效。显然在 socket.io 1.0 中,它是“forceNew”而不是“force new connection”——尽管两者都有效。

Server:

服务器:

var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);

app.get('/', function(req, res){
    res.sendfile('s.html');
});

io.on('connection', function(socket){
    console.log('a user connected: ' + socket.id);
    socket.on('disconnect', function(){
        console.log( socket.name + ' has disconnected from the chat.' + socket.id);
    });
    socket.on('join', function (name) {
        socket.name = name;
        console.log(socket.name + ' joined the chat.');
    });
});

http.listen(3000, function(){
    console.log('listening on *:3000');
});

Client (s.html):

客户端(s.html):

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
    <style>button{width: 100px;}input{width: 300px;}</style>
</head>
<body>

<ul id="messages"></ul>

<button id="disconnect">disconnect</button>
<button id="connect">connect</button>

<script src="/socket.io/socket.io.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script>

    var socket = io.connect('http://localhost:3000');

    $('#disconnect').click(function(){
        socket.disconnect();
    });
    $('#connect').click(function(){
//        socket.socket.reconnect();
//        socket = io.connect('http://localhost:3000',{'force new connection':true });
        socket = io.connect('http://localhost:3000',{'forceNew':true });
        socket.on('connect', function(msg){
            socket.emit('join', prompt('your name?'));
        });
    });
    socket.on('connect', function(msg){
        socket.emit('join', prompt('your name?'));
    });
    socket.on("disconnect", function(){
        console.log("client disconnected from server");
    });

</script>
</body>
</html>