handling multiple net.createConnection() in node.js
the following code goes as so:
var net = require('net'),
querystring = require('querystring'),
http = require('http'),
request = require('request'),
port = 443,
rooms = ['room1','room2'],
index = 0,
request = require('request'),
cheerio = require('cheerio');
for (var i=0;i< rooms.length; i++){
var room = rooms[i]
var url = 'http://xxx.com/server/?group='+room;
request(url, function(err, resp, body) {
if (err)
throw err;
var socket = net.createConnection(port, body);
console.log('Socket created.');
socket.on('data', function(data)
{event_recv(data.toString('utf-8'));}).on('connect', function() {
var auth = myauth + '\x00';
socket.write(auth);
setInterval(function(){
socket.write('\r\n\x00');
}, 60000);
});
})};
function event_recv(data){
console.log(data);
}
i am running this node.js piece locally. what this does is create a socket
based on the host from the list of rooms. the problem is, the first socket
that is made is erased when the second socket is created. thus only the
data from the second socket is received. in python i could manage all of
the created sockets by recording them in to a dict, then using select on
the values(the sockets) and getting the data for each socket in the list.
how would i go about doing this in node.js? (using the code provided).
basically what i am asking is how to save the created sockets so i can
access the received data from each socket.
No comments:
Post a Comment