创建新的连接后,套接字无法连接

时间:2017-02-28 21:57:35

标签: node.js socket.io

首先,我创建了一个连接,所有事件都按预期正常调用。

在结束此连接的功能后,我必须断开连接并等待某些事情发生以创建新连接,这样我就可以发送相同的事件但不同的信息,但是connect事件不再被调用所以其他人都没有发出。

onNewEvent只有在首先连接时才会发生。它第一次有效,但未调用所有下一次尝试connect事件,其他事件reconnect_error,错误和其他事件都没有被调用。

我是否必须保持连接并使用新信息进行发布?

var ioc = require('socket.io-client');

open(1,"User1")
//wait some time
open(2, "User2") // never connects

function open(id, user){
        var client =  ioc.connect(cons.serverSocket, {reconnection: false, "connect timeout":5000});
        client.once("connect", function () { //tried client.on and client.once node is called 2nd time
            console.log('Client: Connected');
            console.log('ID: ' + id + ' USER: ' + user);
            client.emit("onUserLogin", id, user, function (message) {
                console.log('Echo received: ', message);
            });

           client.emit("onNewEvent", cadena, function (message) {
                console.log('Echo received: ', message);
            });

           client.on("onNewEvent", function (emit) {    
                console.log('onNewEvent received ');

                //some functions and callbacks

                setTimeout(function () {
                    console.log(new Date())
                    console.log("liberado!_")
                    liberado = true;
                }, 7 * 1000)

                client.disconnect() // the first connection does call the disconnect event
                //client.io.skipReconnect = false; //also tried
            }) 

        });
        client.on("connect_error", function (emit) {
            console.log('client connect_error received: ' + emit);
        })
        client.on("connect_timeout", function (emit) {
            console.log('client connect_timeout received: ' + emit);
        })
        client.on("reconnect", function (emit) {
            console.log('client reconnect received: ' + emit);
        })
        client.on("reconnect_attempt", function (emit) {
            console.log('client reconnect_attempt received: ' + emit);
        })
        client.on("reconnecting", function (emit) {
            console.log('client reconnecting received: ' + emit);
        })
        client.on("reconnect_error", function (emit) {
            console.log('client reconnect_error received: ' + emit);
        })
        client.on("reconnect_failed", function (emit) {
            console.log('client reconnect_failed received: ' + emit);
        })
        client.on("ping", function (emit) {
            console.log('client ping received: ' + emit);
        })
        client.on("pong", function (emit) {
            console.log('client pong received: ' + emit);
        })
        client.on("error", function (emit) {
            console.log('client error received: ' + emit);
        })
        client.on("disconnect", function (emit) {
            console.log('client disconnect received: ' + emit);
        })

    }
}

0 个答案:

没有答案