Skip to content

Commit

Permalink
Catch exceptions on send()
Browse files Browse the repository at this point in the history
  • Loading branch information
mwittig committed May 3, 2016
1 parent b1034e9 commit 4d61cdc
Showing 1 changed file with 21 additions and 13 deletions.
34 changes: 21 additions & 13 deletions src/discover.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,29 @@ module.exports = function (options) {
discoverer.on('listening', function () {
discoverer.setBroadcast(true);

discoverer.send(discoveryMessage, 0, discoveryMessage.length, port, host, function(error, bytes) {
if (error) {
discoverer.emit('error', error);
try {
if (typeof host !== "string") {
throw new TypeError("invalid arguments: IP address must be a string");
}
else {
debug('UDP message sent to ' + host +':'+ port);
discoverer.send(discoveryMessage, 0, discoveryMessage.length, port, host, function(error, bytes) {
if (error) {
discoverer.emit('error', error);
}
else {
debug('UDP message sent to ' + host +':'+ port);

timeoutId = setTimeout(function() {
try {
discoverer.close();
} catch (ex) {/*ignore*/}
resolve(discoResults);
}, timeout)
}
});
timeoutId = setTimeout(function() {
try {
discoverer.close();
} catch (ex) {/*ignore*/}
resolve(discoResults);
}, timeout)
}
});
}
catch (e) {
discoverer.emit('error', e);
}
});

discoverer.on('message', function (message, remote) {
Expand Down

0 comments on commit 4d61cdc

Please sign in to comment.