I have an array like this:
var exports = module.exports = {};
var net = require('net');
exports.config = [
{
ip: "127.0.0.1",
id: 1,
socket: new net.Socket(),
data: "",
},
{
ip: "192.168.5.242",
id: 2,
socket: new net.Socket(),
data: "",
}
];
I'm trying to connect each of this items with a TCP socket with this code:
for(var key in tornelli.config) {
tornelli.config[key].socket.connect(4000, tornelli.config[key].ip, function() {
console.log('CONNECTED TO: '+tornelli.config[key].ip);
});
tornelli.config[key].socket.on('data', function(data) {
...........
});
tornelli.config[key].socket.on('error', function(error){
console.log(error);
});
}
But somethings get wrong because in the console output I get
CONNECTED TO: 192.168.5.242
But actually I'm connected with '127.0.0.1'. It seems that I don't separate each tcp stream. How can I have two separate tcp stream, one every item in this array?
It's somethings about asynchronous execution?
Thanks