Skip to content

Commit

Permalink
server: ensure host argument is used (#99)
Browse files Browse the repository at this point in the history
Fixes: #98
  • Loading branch information
MylesBorins authored Aug 8, 2023
1 parent 74f53ac commit b0661b3
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 4 deletions.
8 changes: 6 additions & 2 deletions lib/Server.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@ import { EventEmitter } from 'node:events';
import decode from './internal/decode.mjs';

class Server extends EventEmitter {
constructor(port, host, cb) {
constructor(port, host='127.0.0.1', cb) {
super();
if (typeof host === 'function') {
cb = host;
host = '127.0.0.1';
}
if (!cb) cb = () => {};
let decoded;
this.port = port;
Expand All @@ -14,7 +18,7 @@ class Server extends EventEmitter {
type: 'udp4',
reuseAddr: true
});
this._sock.bind(port);
this._sock.bind(port, host);
this._sock.on('listening', () => {
this.emit('listening');
cb();
Expand Down
53 changes: 51 additions & 2 deletions test/test-server.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,32 @@ test('server: create and close', (t) => {
});
});

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

t.plan(3);

t.teardown(() => {
oscServer.close();
client.close();
});

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

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

client.send('/test', (err) => {
t.error(err, 'there should be no error');
});
});

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

t.plan(3);
Expand All @@ -37,6 +61,31 @@ test('client: listen to message', (t) => {
});
});

test('server: callback as second arg', (t) => {
t.plan(4);
const oscServer = new Server(t.context.port, () => {
t.ok('callback called');
});
const client = new Client('127.0.0.1', t.context.port);

t.teardown(() => {
oscServer.close();
client.close();
});

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

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

client.send('/test', (err) => {
t.error(err, 'there should be no error');
});
});

test('server: bad message', (t) => {
t.plan(2);
const oscServer = new Server(t.context.port, '127.0.0.1');
Expand Down

0 comments on commit b0661b3

Please sign in to comment.