From b0661b39bc3910f71d89571d2849aa9cd2b862b0 Mon Sep 17 00:00:00 2001 From: Myles Borins Date: Tue, 8 Aug 2023 13:18:01 -0400 Subject: [PATCH] server: ensure host argument is used (#99) Fixes: https://github.com/MylesBorins/node-osc/issues/98 --- lib/Server.mjs | 8 +++++-- test/test-server.mjs | 53 ++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 57 insertions(+), 4 deletions(-) diff --git a/lib/Server.mjs b/lib/Server.mjs index bb21942..e1893bc 100644 --- a/lib/Server.mjs +++ b/lib/Server.mjs @@ -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; @@ -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(); diff --git a/test/test-server.mjs b/test/test-server.mjs index 19d5216..eceefd7 100644 --- a/test/test-server.mjs +++ b/test/test-server.mjs @@ -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); @@ -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');