Skip to content

Commit fb63090

Browse files
committed
Bug fix: Close discovery socket on error, Improved error handling
1 parent 2f1a5a4 commit fb63090

File tree

1 file changed

+27
-9
lines changed

1 file changed

+27
-9
lines changed

index.js

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -291,17 +291,32 @@ module.exports.discoverDevices = function (options) {
291291
discoverer.bind();
292292

293293
discoverer.on('listening', function () {
294-
discoverer.setBroadcast(true);
295294

296-
discoverer.send(discoveryMessage, 0, discoveryMessage.length, port, host, function(err, bytes) {
297-
if (err) throw err;
298-
debug('UDP message sent to ' + host +':'+ port);
299-
});
295+
try {
296+
discoverer.setBroadcast(true);
300297

301-
timeoutId = setTimeout(function() {
302-
discoverer.close();
303-
resolve(discoResults);
304-
}, timeout)
298+
if (typeof host !== "string") {
299+
throw new TypeError("invalid arguments: IP address must be a string");
300+
}
301+
discoverer.send(discoveryMessage, 0, discoveryMessage.length, port, host, function(error, bytes) {
302+
if (error) {
303+
discoverer.emit('error', error);
304+
}
305+
else {
306+
debug('UDP message sent to ' + host +':'+ port);
307+
308+
timeoutId = setTimeout(function() {
309+
try {
310+
discoverer.close();
311+
} catch (ex) {/*ignore*/}
312+
resolve(discoResults);
313+
}, timeout)
314+
}
315+
});
316+
}
317+
catch (e) {
318+
discoverer.emit('error', e);
319+
}
305320
});
306321

307322
discoverer.on('message', function (message, remote) {
@@ -322,6 +337,9 @@ module.exports.discoverDevices = function (options) {
322337
clearTimeout(timeoutId);
323338
}
324339
debug(error);
340+
try {
341+
discoverer.close();
342+
} catch (ex) {/*ignore*/}
325343
reject(error);
326344
});
327345
});

0 commit comments

Comments
 (0)