Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Broadcast transmission support #130

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
17 changes: 17 additions & 0 deletions lib/Client.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,24 @@ class Client {
type: 'udp4',
reuseAddr: true
});
this._bound = false;
}
setBroadcast(flag, cb) {
if (!cb) cb = () => {};
if (typeof flag !== 'boolean') {
throw new TypeError('flag must be a boolean');
}
if (this._bound) {
this._sock.setBroadcast(flag);
cb();
return;
}
this._sock.bind(this.port, () => {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@MylesBorins Thank you so much!
I can mostly achieve what I want to do.
In my environment, only this part didn’t work properly. It works if I remove the port specification.

this._sock.bind(() => {

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you remove the specified port in the call to bind then node will just pick a random port, that really isn't desirable functionality. Are you getting an error?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand this specification. No problem!

Because I was testing sending and receiving on the same PC, the port number was duplicated and the callback was not called.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@narikei do you have an example script of what you are trying to do that I can play with? I'm not running into the same problem regarding duplicated port number and binding between server + client in my tests.

this._bound = true;
this._sock.setBroadcast(flag);
cb();
})
}
close(cb) {
this._sock.close(cb);
}
Expand Down
32 changes: 32 additions & 0 deletions test/test-client.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,38 @@ 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) => {
client.setBroadcast(false);
t.error(err, 'there should be no error');
client.close();
});
});

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

t.plan(1);

t.throws(() => {
client.setBroadcast('Oops');;
});

client.close();
});

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

Expand Down
Loading