sockets - How to keep a tcp connection always open with node.js -
i'm building tcp-message server nodejs. sits on server waiting accept connection lets arduino.
as it, @ connection-time, identifies unique id (not ip) i'm able write data server > arduino without knowing ip address of client-device.
but efficient, want connection open long possible, preferably long client-device closes connection. (eg on ip change or something)
this (relevant part of) server:
var net = require('net'), sockets = {}; var tcp = net.createserver(function(soc){ soc.setkeepalive(true); //- 1 soc.on('connect', function(data){ soc.setkeepalive(true); //- 2 }); soc.on('data', function(data) { //- stuff data, , after identification // store in sockets{} }) }).listen(1111);
is soc.setkeepalive(true)
right method keep connection alive? if so, right place put it? in connect event (1), or right in callback (2).
if not way it, is?
your best bet periodically send own heartbeat messages.
also, don't need soc.on('connect', ...)
because client socket connected when callback executed.
Comments
Post a Comment