Skip to content

Commit

Permalink
fixup: avoid edge cases of double bound
Browse files Browse the repository at this point in the history
  • Loading branch information
MylesBorins committed Dec 14, 2024
1 parent 4cb962a commit 1babf99
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
15 changes: 12 additions & 3 deletions lib/Client.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,21 @@ class Client {
type: 'udp4',
reuseAddr: true
});
this._bound = false;
}
setBroadcast(flag) {
setBroadcast(flag, cb) {
if (!cb) cb = () => {};
if (this._bound) {
this._sock.setBroadcast(flag);
cb();
return;
}
this._sock.bind(this.port, () => {
this._bound = true;
this._sock.setBroadcast(flag);
})
}
cb();
})
}
close(cb) {
this._sock.close(cb);
}
Expand Down
1 change: 1 addition & 0 deletions test/test-client.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ test('client: setBroadcast', (t) => {
});

client.send(['/test', 0, 1, 'testing', true], (err) => {
client.setBroadcast(false);
t.error(err, 'there should be no error');
client.close();
});
Expand Down

0 comments on commit 1babf99

Please sign in to comment.