node-json-socket icon indicating copy to clipboard operation
node-json-socket copied to clipboard

client is not connecting to multiple servers in different machines with diff IP and port

Open muthurajbharathi opened this issue 7 years ago • 3 comments

Hi i have implemented server like below in 10 different machines. and i can see "connected jsonsocketserver" getting printed in all machines when client connects but not receiving on "message" event. please check my server and client code.

var JsonSocket = require('json-socket'); var JsonSocketserver = net.createServer(); JsonSocketserver.listen(rBoxPort2); JsonSocketserver.on('connection', function(sock) { //This is a standard net.Socket log("connected jsonsocketserver"); js = new JsonSocket(sock); //Now we've decorated the net.Socket to be a JsonSocket js.on('message', function(message) { try { var obj = JSON.parse(message); if (obj.hasOwnProperty('server-sw-check')) { console.log('server-sw-check'); if (obj.hasOwnProperty('deviceArr')) { checkDeviceSWversion(obj.deviceArr, function(data) { log('data '+data); js.sendMessage(JSON.stringify(data)); }); } } } catch (err) { log("jason sock on msg err is : " + err); } });

js.on('end', function() {
    log(" jason sock end "); //after end close will happen
});
js.on('error', function(error) {
	log('jason sock error :');
	log(error);

});
js.on('close', function() {
	log(" jason sock close ");
});
sock.on('drain', function() {
	log('sock drain..');
    //i might have to handle something here....
});
sock.on('timeout', function() {
	log('sock timeout : ');
    //i might have to handle something here....
});

}); JsonSocketserver.on('error', function(e) { if (e.code == 'EADDRINUSE') { log('CRITICAL ERROR : JsonSocketserver Address in use, retrying...');

	} else {
		log('CRITICAL ERROR : JsonSocketserver SERVER ERROR - ' + e.code);
	}
});

JsonSocketserver.on('end', function() {
	log('JsonSocketserver SERVER END');

});

JsonSocketserver.on('close', function() {
	log('JsonSocketserver SERVER CLOSE');

});

And i have the server in 10 different machines with different IP my client code is as below. In a for loop i am connecting to 10 different server but data is sent to only one machine 9 times. means jsonSocket.sendMessage(query); is sending data to only one machine but it is receiving jsonSocket.on('message', function(message) { from 9 machines.

for (var i = 0; i < devIpArr.length; i++) { (function(i) { var plugStat = true; var JsonSocket = require('json-socket'); var netSocket = new net.Socket(); jsonSocket = new JsonSocket(netSocket); //Decorate a standard net.Socket with JsonSocket jsonSocket.connect(RBOX_PORT2, devIpArr[i]);

                                jsonSocket.on('connect', function(sock) { //Don't send until we're connected 
                                    var query = {
                                        'server-sw-check': 'yes',
                                        'deviceArr': devObj
                                    };
                                    query = JSON.stringify(query);
                                    jsonSocket.sendMessage(query);
                                    
                                });

                                netSocket.setKeepAlive(true);
                                netSocket.setNoDelay(true);
                               
                                jsonSocket.on('error', function(err) {
                                    console.log(err);
                                    log('deviceVersionCheck : Connect to rBox with ip ' + devIpArr[i] + ' failed ' + err);
                                    if (plugStat) {
                                        handleDoneIp();
                                        plugStat = false;
                                    }
                                })

                                jsonSocket.on('message', function(message) {
                                    var devArr = [];
                                    log(' data '+message);  
                                    try {
                                        devArr = JSON.parse(message);
                                        for (var j = 0; j < devArr.length; j++) {
                                            (function(j) {
                                                ticket_pool.getConnection(function(err, connection_int) {
                                                    if (err) {
                                                        log('deviceVersionCheck : Could not get Second DB Connection - ' + devArr[j]);
                                                    } else {
                                                        var value = devArr[j] + '-updating';
                                                        var sql = 'UPDATE pcloud_device SET device_id = "' + value + '" , rbid = 0 WHERE device_id = "' + devArr[j] + '"';
                                                        connection_int.query(sql, function(err, srows) {
                                                            if (err) {
                                                                log('deviceVersionCheck : Update DB Failed - ' + devArr[j]);
                                                            }
                                                            connection_int.release();
                                                        });
                                                    }
                                                });
                                            })(j);
                                        }
                                    } catch (err) {
                                        log('deviceVersionCheck : CATCH - JSON parse from rBox with ip ' + devIpArr[i] + ' failed ' + err);
                                    }

                                    netSocket.destroy();
                                    delete netSocket;

                                    if (plugStat) {
                                        handleDoneIp();
                                        plugStat = false;
                                    }
                                });

                                jsonSocket.on('end', function(message) {

                                    log('jsonSocket client end');
                                }) 

                                jsonSocket.on('close', function(message) {

                                    log('jsonSocket client close');
                                })
                            })(i);

muthurajbharathi avatar Mar 15 '17 10:03 muthurajbharathi

This is hard to debug from here. Can you make a more minimal example describing your problem? Thanks

sebastianseilund avatar Mar 15 '17 15:03 sebastianseilund

Actual problem is i have one json-socket client and multiple json-socket servers. And i have a for loop to connect to multiple servers and send data but client is sending data only to one server multiple times.

muthurajbharathi avatar Mar 16 '17 06:03 muthurajbharathi

You're missing var to declare jsonSocket, so it's becoming a global variable. It's missing in the server example in the documentation because it's overwriting an argument.

1j01 avatar Apr 08 '17 04:04 1j01