【问题标题】:node net socket connection timeout节点网络套接字连接超时
【发布时间】:2015-06-04 02:14:48
【问题描述】:

我注意到,我的节点服务器的 net.createConnection() 在某些情况下触发错误之前有很长的超时时间(似乎是端口的特殊问题......)

我尝试连接到 somedomain:9000(按预期收听、连接和工作) 和 somedomain:1234 (相同的域,不同的端口,等待大约 2 分钟,直到“连接 ETIMEDOUT”)

当我连接到不存在的域时,我会立即收到错误消息,但如果我连接到可访问主机上的不可访问端口则不会。 我需要确定一台机器是否可以在

我该如何处理?必须以某种方式在 2 分钟内发现无法访问的端口? 至少某种超时只是将地址设置为在一定时间后无法访问?

谢谢

更新:当前连接代码:

this.openConnection = function() {
    try {
        console.log("[INFO] connecting to " + device.ip + ":" + device.port);
        device.clientSocket = new net.createConnection(this.port,this.ip)
            .on('connect', device.connected)
            .on('data', device.inputReceived)
            .on('error', function(err) {

                if (err.code == "ENOTFOUND") {
                    console.log("[ERROR] No device found at this address!");
                    device.clientSocket.destroy();
                    return;
                }

                if (err.code == "ECONNREFUSED") {
                    console.log("[ERROR] Connection refused! Please check the IP.");
                    device.clientSocket.destroy();
                    return;
                }


                console.log("[CONNECTION] Unexpected error! " + err.message + "     RESTARTING SERVER");


                process.exit(1);

            })
            .on('disconnect', function() {
                console.log("[CONNECTION] disconnected!");
            });
    } catch(err) {
        console.log("[CONNECTION] connection failed! " + err);
    }
};

【问题讨论】:

    标签: node.js sockets timeout


    【解决方案1】:

    当你连接时,你可以设置你自己的定时器来设置你想要的任何超时时间,如果在定时器触发时连接没有成功,那么它就不会像你想要的那样快速成功。

    这可以封装在带有单个回调或返回承诺的单个函数中。


    根据您的代码,这里有一个添加超时的镜头(未经测试的代码):

    this.openConnection = function(timeout) {
        var timer;
        timeout = timeout || 2000;
        try {
            console.log("[INFO] connecting to " + device.ip + ":" + device.port);
            device.clientSocket = new net.createConnection(this.port,this.ip)
                .on('connect', function() {
                    clearTimeout(timer);
                    device.connected();
                })
                .on('data', function() {
                    clearTimeout(timer);
                    device.inputReceived();
                })
                .on('error', function(err) {
                    clearTimeout(timer);
                    if (err.code == "ENOTFOUND") {
                        console.log("[ERROR] No device found at this address!");
                        device.clientSocket.destroy();
                        return;
                    }
    
                    if (err.code == "ECONNREFUSED") {
                        console.log("[ERROR] Connection refused! Please check the IP.");
                        device.clientSocket.destroy();
                        return;
                    }
    
    
                    console.log("[CONNECTION] Unexpected error! " + err.message + "     RESTARTING SERVER");
    
    
                    process.exit(1);
    
                })
                .on('disconnect', function() {
                    console.log("[CONNECTION] disconnected!");
                });
            timer = setTimeout(function() {
                console.log("[ERROR] Attempt at connection exceeded timeout value");
                device.clientSocket.end();
            }, timeout);
        } catch(err) {
            console.log("[CONNECTION] connection failed! " + err);
        }
    };
    

    【讨论】:

    • 我需要设置一个标志,向用户显示此设备是在线还是离线。你会怎么做呢?我想,一个承诺是行不通的,因为我事先不知道状态会是什么,而且不会错。只是一个简单的 setTimeout?
    • @PaulSchneider - 告诉我你当前的连接代码,你检测到连接失败的地方,我可以告诉你如何调整它以添加你自己的计时器。
    • @PaulSchneider - 我添加了一个带有超时的版本。
    • 直到星期六才能测试它......也许明天。我会告诉你的,谢谢:)
    • @PaulSchneider - 这是代码中的错字。到处都是clearTimeout(timeout);,应该是clearTimeout(timer);,我更正了答案中的代码。
    猜你喜欢
    • 1970-01-01
    • 2016-04-18
    • 2011-05-28
    • 2012-03-28
    • 1970-01-01
    • 2011-05-11
    • 1970-01-01
    • 1970-01-01
    • 2018-03-26
    相关资源
    最近更新 更多