Skip to content

Commit

Permalink
'networkInterface' event (#50)
Browse files Browse the repository at this point in the history
* feat: 'networkInterface' event

* chore: lint issue
  • Loading branch information
richardschneider authored Oct 4, 2021
1 parent 309a1aa commit 5aa11f4
Showing 1 changed file with 24 additions and 7 deletions.
31 changes: 24 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,18 @@ module.exports = function (opts) {
if (destroyed) return process.nextTick(cb)
destroyed = true
clearInterval(interval)
socket.once('close', cb)
socket.close()

// Need to drop memberships by hand and ignore errors.
// socket.close() does not cope with errors.
for (var iface in memberships) {
try {
socket.dropMembership(ip, iface)
} catch (e) {
// eat it
}
}
memberships = {}
socket.close(cb)
}

that.update = function () {
Expand All @@ -126,20 +136,27 @@ module.exports = function (opts) {

for (var i = 0; i < ifaces.length; i++) {
var addr = ifaces[i]

if (memberships[addr]) continue
memberships[addr] = true
updated = true

try {
socket.addMembership(ip, addr)
memberships[addr] = true
updated = true
} catch (err) {
that.emit('warning', err)
}
}

if (!updated || !socket.setMulticastInterface) return
socket.setMulticastInterface(opts.interface || defaultInterface())
if (updated) {
if (socket.setMulticastInterface) {
try {
socket.setMulticastInterface(opts.interface || defaultInterface())
} catch (err) {
that.emit('warning', err)
}
}
that.emit('networkInterface')
}
}

return that
Expand Down

0 comments on commit 5aa11f4

Please sign in to comment.