node.js - clients are continously added to socket due to server assigning ip automatically -
i have problem code have 3 clients(device) connected socket server (node.js). every time clients disconnected , connected, clients array length increasingly take 50 clients or more.i tried put 'end' emitter outside in 'data' still continuously adding clients.i don't know wrong code.
i hope me on this.
var net = require('net'); var clients = []; var server = net.createserver(function(socket){ socket.name = socket.remoteaddress; if(!checkarray(socket.name,clients)){ clients.push(socket); }else{ console.log("client exist"); } socket.on('data',function(data){ var revdata = data.tostring(); // dosomething(revdata); socket.on('end', function () { var indexremove = clients.indexof(socket); if (indexremove > -1) { clients.splice(indexremove, 1); console.log("clients are"+ clients.length); } }); }); }); function checkarray(value, array) { return array.indexof(value) > -1; } server.listen(3020,'127.0.0.1', function () { console.log("server up!"); });
it might have bug in first call checkarray
. you're mistakenly using socket.name
first parameter when should using socket
itself. more specifically, logic checks if client has connected, try this:
if(!checkarray(socket,clients)){ clients.push(socket); }else{ console.log("client exist"); }
Comments
Post a Comment