Skip to content

Commit

Permalink
feat: add setBroadcast
Browse files Browse the repository at this point in the history
This exposes a new `setBroadcast` feature on the client with
an optional callback. Tests + docs included.
  • Loading branch information
MylesBorins committed Dec 14, 2024
1 parent be7016c commit 4cb962a
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 7 deletions.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,20 @@ oscServer.on('bundle', function (bundle) {
});
```

### Enabling Broadcast on Client

**WARNING**: Broadcast support is Experimental and subject to change at any point.

```js
import { Client } from 'node-osc';

const client = new Client('127.0.0.1', 3333);
client.setBroadcast(true);
client.send('/oscAddress', 200, () => {
client.close();
});
```

### CJS API

This just works due to conditional exports, isn't that cool!
Expand Down
13 changes: 6 additions & 7 deletions lib/Client.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,18 @@ import Message from './Message.mjs';
const { toBuffer } = oscMin;

class Client {
constructor(host, port, isBroadcast = false) {
constructor(host, port) {
this.host = host;
this.port = port;
this._sock = createSocket({
type: 'udp4',
reuseAddr: true
});

if (isBroadcast) {
this._sock.bind(() => {
this._sock.setBroadcast(true);
});
}
}
setBroadcast(flag) {
this._sock.bind(this.port, () => {
this._sock.setBroadcast(flag);
})
}
close(cb) {
this._sock.close(cb);
Expand Down
19 changes: 19 additions & 0 deletions test/test-client.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,25 @@ test('client: with Bundle object', (t) => {
});
});

test('client: setBroadcast', (t) => {
const oscServer = new Server(t.context.port, '127.0.0.1');
const client = new Client('127.0.0.1', t.context.port);

t.plan(2);

client.setBroadcast(true);

oscServer.on('message', (msg) => {
oscServer.close();
t.same(msg, ['/test', 0, 1, 'testing', true], 'We should receive expected payload');
});

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

test('client: failure', (t) => {
const client = new Client('127.0.0.1', t.context.port);

Expand Down

0 comments on commit 4cb962a

Please sign in to comment.