44

disconnect一度呼び出されたソケットioに再接続するにはどうすればよいですか?

これがコードです

function initSocket(__bool){                    
    if(__bool == true){             
        socket = io.connect('http://xxx.xxx.xxx.xxx:8081', {secure:false});     
        socket.on('connect', function(){console.log('connected')});                                 
        socket.on('disconnect', function (){console.log('disconnected')});
    }else{
        socket.disconnect();
        socket = null;
    }
}   

私がそうするならばinitSocket(true)、それは働きます。私がそうするinitSocket(false)と、それは切断されます。しかし、を使用して再接続しようとするとinitSocket(true)、接続が機能しなくなります。接続を機能させるにはどうすればよいですか?

4

5 に答える 5

51

さて、ここにオプションがあります...

接続する必要があるソケット値を初めて初期化するときはio.connect

次回 (一度 disconnect を呼び出した後)、 に接続し直す必要がありますsocket.socket.connect()

だからあなたのinitSocket、のようなものでなければなりません

function initSocket(__bool){                    
    if(__bool){          
        if ( !socket ) {   
            socket = io.connect('http://xxx.xxx.xxx.xxx:8081', {secure:false});     
            socket.on('connect', function(){console.log('connected')});                                 
            socket.on('disconnect', function (){console.log('disconnected')});
        } else {
            socket.socket.connect(); // Yep, socket.socket ( 2 times )
        }
    }else{
        socket.disconnect();
        // socket = null; <<< We don't need this anymore
    }
} 
于 2012-05-04T05:26:23.260 に答える
16

すでに回答があることは承知していますが、現在 node で socket.IO クライアントの再接続機能が壊れているため、ここにたどり着きました。

github リポジトリで進行中のバグによると、多くの人が接続失敗時にイベントを取得しておらず、再接続が自動的に行われていないことが示されています。

これを回避するには、次のように手動再接続ループを作成します。

var socketClient = socketioClient.connect(socketHost)

var tryReconnect = function(){

    if (socketClient.socket.connected === false &&
        socketClient.socket.connecting === false) {
        // use a connect() or reconnect() here if you want
        socketClient.socket.connect()
   }
}

var intervalID = setInterval(tryReconnect, 2000)

socketClient.on('connect', function () {
    // once client connects, clear the reconnection interval function
    clearInterval(intervalID)
    //... do other stuff
})
于 2012-11-29T15:58:38.547 に答える
5

クライアント側の設定に従って再接続できます。

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

// 1.0 socket.io version
io.connect(SERVER_IP,{'forceNew':true });
于 2014-11-24T14:50:43.833 に答える
0

I had an issue with socket-io reconnect. May be this case will help someone. I had code like this:

var io = require('socket.io').listen(8080);
DB.connect(function () {
    io.sockets.on('connection', function (socket) {
        initSockets(socket);
    });
});

this is wrong, becase there is a delay between open port assigned callbacks. Some of messages may be lost before DB gets initialized. The right way to fix it is:

var io = null;
DB.connect(function () {
    io = require('socket.io').listen(8080);
    io.sockets.on('connection', function (socket) {
        console.log("On connection");
        initSockets(socket);
    });
});
于 2015-10-07T15:16:51.777 に答える