Nodejs - 让客户端套接字在5秒超时后再次尝试 [英] Nodejs - getting client socket to try again after 5 sec time out

查看:65
本文介绍了Nodejs - 让客户端套接字在5秒超时后再次尝试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

刚开始使用node.js编程并编写tcp套接字客户端。

Just starting out in node.js programming and writing a tcp socket client.

我希望客户端连接到服务器。如果服务器不可用(即服务器不存在于约定的端口),我希望客户端超时并在超时后重新连接。

I want the client to connect to a server. If the server is not available (i.e. server does not exist at a agreed port), i want the client to timeout and reconnect after the timeout.

我有这个代码但是它挂在第二个client.connect。怎么了?

I have this code but it hangs at the second client.connect. What's wrong?

var net = require('net');
var HOST = '127.0.0.1';
var PORT = 9000;
var client = new net.Socket();

client.connect(PORT, HOST, function(){
    console.log('CONNECTED TO: ' + HOST + ':' + PORT);
    client.write('I am Superman!');
});

client.on('error', function(e) {
    while (e.code == 'ECONNREFUSED') {
        console.log('Is the server running at ' + PORT + '?');`

        socket.setTimeout(1000, function() {
            console.log('Timeout for 5 seconds before trying port:' + PORT + ' again');
        }

        client.connect(PORT, HOST, function(){
            console.log('CONNECTED TO: ' + HOST + ':' + PORT);
            client.write('I am the inner superman');
        });
    }); 
});

更新代码:

var net = require('net');
var HOST = '127.0.0.1';
var PORT = 9000;
var client = new net.Socket();

client.connect(PORT, HOST, function(){
    console.log('CONNECTED TO: ' + HOST + ':' + PORT);
    client.write('I am Superman');
});

client.on('error', function(e) {

    while (e.code == 'ECONNREFUSED') {
        console.log('Is the server running at ' + PORT + '?');

        client.setTimeout(4000, function() {

            client.connect(PORT, HOST, function() {
                console.log('CONNECTED TO: ' + HOST + ':' + PORT);
                client.write('I am inner Superman');
            });         

            console.log('Timeout for 5 seconds before trying port:' + PORT + ' again');
        });
    }
});

client.on('data', function(data) {
    console.log('DATA: ' + data);
    client.destroy();
});

client.on('close', function() {
    console.log('Connection closed');
});

使用更新的代码,超时似乎不会生效。当我启动此客户端没有相应的服务器时,结果显示如下,没有4秒等待。

With the updated code, the timeout does not appear to take effect. When i start this client with no corresponding server, the result shows below with no 4 second wait.

Is the server running at 9000?
Is the server running at 9000?
Is the server running at 9000?
Is the server running at 9000?
…

更新(咆哮错误的树?)

Update (Barking up the wrong tree?)

我回去查看socket.on('error')事件,发现错误后立即调用close事件。所以代码将关闭tcpclient而不等待4秒。还有更好的想法吗?

I went back to look at the socket.on('error') event and saw that the close event is called immediately after the error. So the code will close out the tcpclient without waiting for 4 seconds. Any better ideas?

推荐答案

你的超时时间是相反的。

You're timeout is reversed.

应该看起来像:

var net = require('net');
var HOST = '127.0.0.1';
var PORT = 9000;
var client = new net.Socket();

client.connect(PORT, HOST, function(){
    console.log('CONNECTED TO: ' + HOST + ':' + PORT);
    client.write('I am Superman!');
});

client.on('error', function(e) {
    if(e.code == 'ECONNREFUSED') {
        console.log('Is the server running at ' + PORT + '?');

        client.setTimeout(4000, function() {
            client.connect(PORT, HOST, function(){
                console.log('CONNECTED TO: ' + HOST + ':' + PORT);
                client.write('I am the inner superman');
            });
        });

        console.log('Timeout for 5 seconds before trying port:' + PORT + ' again');

    }   
});
client.on('data', function(data) {
    console.log('DATA: ' + data);
    client.destroy();
});
client.on('close', function() {
    console.log('Connection closed');
});

超时后要运行的函数是回调。那是等待执行的那个。

The function you want to run after the timeout is the callback. That's the one that waits for execution.

另外,将改为改为如果,在单个错误事件期间,该条件不会更改。你的parens和括号不匹配。

Also, change your while to an if, that condition won't change during a single error event. And your parens and brackets are mismatched.

这篇关于Nodejs - 让客户端套接字在5秒超时后再次尝试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆