socket.io client connect disconnect

后端 未结 2 1533
生来不讨喜
生来不讨喜 2021-02-01 06:03

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

Server side code:

io.on(\'connection\', f         


        
2条回答
  •  有刺的猬
    2021-02-01 06:25

    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.

    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):

    
    
    
        
        
        
    
    
    
    

    提交回复
    热议问题