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

add multicastInterface option for specifying which interface to use #22

Open
wants to merge 1 commit into
base: master
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
30 changes: 27 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,15 @@ The Client class implements a UDP client for sending E1.31 (sACN) traffic. The c

```javascript
var e131 = require('e131');
var client = new e131.Client(arg, [port]);
var client = new e131.Client(arg, [port], {multicastInterface:'192.168.0.2'});
```

The first argument can be a host address, name or universe number. If `port` is omitted, the default E1.31 port `5568` is used.
If a universe is given, the client will automatically join the relevant Multicast group.
The first argument can be a host address, name or universe number. If a universe is given, the client will automatically join the relevant Multicast group.

If `port` is omitted, the default E1.31 port `5568` is used.

If you have multiple network interface you can use `multicastInterface` option to specify which one to use. The value should be the ip address of the outgoing network interface, for example `192.168.0.2`.

The client automatically increments (and wraps around if necessary) the sequence number of the transmitted packet.

The client provides two methods:
Expand Down Expand Up @@ -100,6 +104,26 @@ server.on('packet', function (packet) {
});
```

If you have multiple network interfaces you can specify which interface to use with the multicastInterface option
```javascript
var e131 = require('e131');

var server = new e131.Server([0x0001, 0x0002], undefined, {multicastInterface:'192.168.0.2'});
server.on('listening', function() {
console.log('server listening on interface %s, port %d, universes %j', this.multicastInterface, this.port, this.universes);
});
server.on('packet', function (packet) {
var sourceName = packet.getSourceName();
var sequenceNumber = packet.getSequenceNumber();
var universe = packet.getUniverse();
var slotsData = packet.getSlotsData();

console.log('source="%s", seq=%d, universe=%d, slots=%d',
sourceName, sequenceNumber, universe, slotsData.length);
console.log('slots data = %s', slotsData.toString('hex'));
});
```

## E1.31 (sACN) Packet Class

The E1.31 Packet class contains a number of useful setter methods:
Expand Down
11 changes: 10 additions & 1 deletion lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,27 @@ var dgram = require('dgram');
var e131 = require('./e131');

// E1.31 client object constructor
function Client(arg, port) {
function Client(arg, port, options) {
if (this instanceof Client === false) {
return new Client(arg, port);
}

const default_options = {
multicastInterface: '0.0.0.0'
}
options = Object.assign({}, default_options, options)

if (arg === undefined) {
throw new TypeError('arg should be a host address, name or universe');
}

this.host = Number.isInteger(arg) ? e131.getMulticastGroup(arg) : arg;
this.port = port || e131.DEFAULT_PORT;
this._socket = dgram.createSocket('udp4');

this._socket.bind(port, () => {
this._socket.setMulticastInterface(options.multicastInterface);
});
}

// create a new E1.31 packet
Expand Down
17 changes: 14 additions & 3 deletions lib/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,23 @@ var EventEmitter = require('events').EventEmitter;
var e131 = require('./e131');

// E1.31 server object constructor
function Server(universes, port) {
function Server(universes, port, options) {
if (this instanceof Server === false) {
return new Server(port);
}
EventEmitter.call(this);

const default_options = {
multicastInterface: undefined
}
options = Object.assign({}, default_options, options);

if (universes !== undefined && !Array.isArray(universes)) {
universes = [universes];
}
this.universes = universes || [0x01];
this.port = port || e131.DEFAULT_PORT;
this.multicastInterface = options.multicastInterface;
this._socket = dgram.createSocket('udp4');
this._lastSequenceNumber = {};

Expand Down Expand Up @@ -67,10 +73,15 @@ function Server(universes, port) {
}
self._lastSequenceNumber[packet.getUniverse()] = packet.getSequenceNumber();
});
this._socket.bind(this.port, function onListening() {
this._socket.bind(this.port, this.multicastInterface, function onListening() {
self.universes.forEach(function (universe) {
var multicastGroup = e131.getMulticastGroup(universe);
self._socket.addMembership(multicastGroup);

if (self.multicastInterface === undefined) {
self._socket.addMembership(multicastGroup);
} else {
self._socket.addMembership(multicastGroup, self.multicastInterface);
};
});
});
}
Expand Down