-
• #2
OK, I've succeded with the following code:
connect-to-server = !-> csock = null connect-to = (server) -> console.log "trying to connect to the server...", server csock := net.connect server, (socket) !-> console.log "client connected. " socket.on \data, (data) !-> do-something! socket.on \end, !-> console.log "client disconnected..." connect-to first-server <- sleep 10_000ms if csock.sckt is void console.log "timed out, trying default server..." connect-to default-server else console.log "Connected to the first server correctly, not trying default..."
...Javascript:
var connectToServer; connectToServer = function(){ var csock, connectTo; csock = null; connectTo = function(server){ console.log("trying to connect to the server...", server); return csock = net.connect(server, function(socket){ console.log("client connected. "); socket.on('data', function(data){ doSomething(); }); socket.on('end', function(){ console.log("client disconnected..."); }); }); }; connectTo(firstServer); sleep(10000, function(){ if (csock.sckt === void 8) { console.log("timed out, trying default server..."); return connectTo(defaultServer); } else { return console.log("Connected to the first server correctly, not trying default..."); } }); };
-
• #3
You might be able to do:
csock = net.connect(server, function(socket){ }); csock.on("error", function(info) { ... });
I think socket connections should time out eventually - it might take maybe 30 seconds or a minute though?
-
• #4
Yeah - it's an awkwardly long timeout. So if you're updating it too often (eg, in an interval), if the server goes down, it hoses everything.
That's why I can't turn on some of the lights in my room if the webserver on the shelf in the living room isn't running.
Hi,
I found this conversation but I didn't find my answer in the replies.
When I try to connect to a valid server, my application works as expected and exchanges data. If I try to connect to an invalid server, for example
net.connect({host: "hello", port: 123}, function () {....
, then it hangs while trying to connect.How can I implement a timeout or something in order to fall back to a default server?