我需要建立3个tcp连接,很难分辨回调中哪个连接是成功的" connect"事件
var clients = [];
var ports = [81,82,83];
for (i=0; i<3; i++) {
clients[i] = net.createConnection(ports[i], '127.0.0.1');
clients[i].on('connect', function(conn) {
console.log("connect is setup");
console.log(conn); //it's always undefined, why???
//need to set different data to the different connections
});
}
答案 0 :(得分:1)
提供给connect
事件的参数是潜在的连接错误,因此,您要检查是否存在错误。如果我没记错,console.log(this);
有关于套接字的信息。您对套接字的主要引用也是clients[i]