node.js - In nodejs, how do I check if a port is listening or in use -
i'll specific here in hope folks understand can edit rephrase general situation.
currently when run "node debug", spawns process listen on port 5858. in parent, connection attempted port.
however if have "node debug" session running, "node debug" hangs because port in use.
specifically message see is:
$ node debug example/gcd.js 3 5 < debugger listening on port 5858 > connecting...
better detect port in use (without connecting might mess client trying connect existing debugger).
edit: accepted solution in trepanjs.
see node js - how can tell if socket open einaros ws socket module?
a variation on following used:
var net = require('net'); var portinuse = function(port, callback) { var server = net.createserver(function(socket) { socket.write('echo server\r\n'); socket.pipe(socket); }); server.listen(port, '127.0.0.1'); server.on('error', function (e) { callback(true); }); server.on('listening', function (e) { server.close(); callback(false); }); }; portinuse(5858, function(returnvalue) { console.log(returnvalue); });
the actual commit little more involved https://github.com/rocky/trepanjs/commit/f219410d72aba8cd4e91f31fea92a5a09c1d78f8
Comments
Post a Comment