Skip to content

Commit

Permalink
Merge pull request #14 from adzialocha/fix/plugin-method-args
Browse files Browse the repository at this point in the history
Fix: Plugin method arguments
  • Loading branch information
adzialocha authored Oct 8, 2017
2 parents f567ba2 + 9569953 commit 2b0c7ae
Show file tree
Hide file tree
Showing 8 changed files with 93 additions and 51 deletions.
48 changes: 30 additions & 18 deletions dist/osc.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/osc.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/osc.min.js

Large diffs are not rendered by default.

48 changes: 30 additions & 18 deletions lib/osc.js
Original file line number Diff line number Diff line change
Expand Up @@ -893,7 +893,12 @@ var STATUS$1 = {
type: 'udp4',
open: defaultOpenOptions,
send: defaultSendOptions
};
};function mergeOptions(base, custom) {
return Object.assign({}, defaultOptions$2, base, custom, {
open: Object.assign({}, defaultOptions$2.open, base.open, custom.open),
send: Object.assign({}, defaultOptions$2.send, base.send, custom.send)
});
}
var DatagramPlugin = function () {
function DatagramPlugin() {
var _this = this;
Expand All @@ -902,7 +907,7 @@ var DatagramPlugin = function () {
if (!dgram) {
throw new Error('DatagramPlugin can not be used in browser context');
}
this.options = Object.assign({}, defaultOptions$2, customOptions);
this.options = mergeOptions({}, customOptions);
this.socket = dgram.createSocket(this.options.type);
this.socketStatus = STATUS$1.IS_NOT_INITIALIZED;
this.socket.on('message', function (message) {
Expand All @@ -928,7 +933,7 @@ var DatagramPlugin = function () {
value: function open() {
var _this2 = this;
var customOptions = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
var options = Object.assign({}, this.options.openOptions, customOptions);
var options = Object.assign({}, this.options.open, customOptions);
var port = options.port,
exclusive = options.exclusive;
this.socketStatus = STATUS$1.IS_CONNECTING;
Expand All @@ -955,7 +960,7 @@ var DatagramPlugin = function () {
key: 'send',
value: function send(binary) {
var customOptions = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
var options = Object.assign({}, this.options.sendOptions, customOptions);
var options = Object.assign({}, this.options.send, customOptions);
var port = options.port,
host = options.host;
this.socket.send(new Buffer(binary), 0, binary.byteLength, port, host);
Expand All @@ -965,7 +970,7 @@ var DatagramPlugin = function () {
}();

var dgram$1 = typeof __dirname !== 'undefined' ? require('dgram') : undefined;
var WebSocket$1 = typeof __dirname !== 'undefined' ? require('ws').Server : undefined;
var WebsocketServer = typeof __dirname !== 'undefined' ? require('ws').Server : undefined;
var STATUS$2 = {
IS_NOT_INITIALIZED: -1,
IS_CONNECTING: 0,
Expand All @@ -987,7 +992,7 @@ var STATUS$2 = {
port: 8080
},
receiver: 'ws'
};function mergeOptions(base, custom) {
};function mergeOptions$1(base, custom) {
return Object.assign({}, defaultOptions$3, base, custom, {
udpServer: Object.assign({}, defaultOptions$3.udpServer, base.udpServer, custom.udpServer),
udpClient: Object.assign({}, defaultOptions$3.udpClient, base.udpClient, custom.udpClient),
Expand All @@ -999,10 +1004,10 @@ var BridgePlugin = function () {
var _this = this;
var customOptions = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
classCallCheck(this, BridgePlugin);
if (!dgram$1 || !WebSocket$1) {
if (!dgram$1 || !WebsocketServer) {
throw new Error('BridgePlugin can not be used in browser context');
}
this.options = mergeOptions({}, customOptions);
this.options = mergeOptions$1({}, customOptions);
this.websocket = null;
this.socket = dgram$1.createSocket('udp4');
this.socketStatus = STATUS$2.IS_NOT_INITIALIZED;
Expand Down Expand Up @@ -1030,15 +1035,22 @@ var BridgePlugin = function () {
value: function open() {
var _this2 = this;
var customOptions = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
var options = mergeOptions(this.options, customOptions);
var options = mergeOptions$1(this.options, customOptions);
this.socketStatus = STATUS$2.IS_CONNECTING;
this.socket.bind({
address: options.udpServer.host,
port: options.udpServer.port,
exclusive: options.udpServer.exclusive
}, function () {
_this2.websocket = new WebSocket$1({ host: options.wsServer.host, port: options.wsServer.port });
_this2.websocket = new WebsocketServer({
host: options.wsServer.host,
port: options.wsServer.port
});
_this2.websocket.binaryType = 'arraybuffer';
_this2.websocket.on('listening', function () {
_this2.socketStatus = STATUS$2.IS_OPEN;
_this2.notify('open');
});
_this2.websocket.on('error', function (error) {
_this2.notify('error', error);
});
Expand All @@ -1048,7 +1060,6 @@ var BridgePlugin = function () {
_this2.notify(new Uint8Array(message));
});
});
_this2.notify('open');
});
}
}, {
Expand All @@ -1057,16 +1068,17 @@ var BridgePlugin = function () {
var _this3 = this;
this.socketStatus = STATUS$2.IS_CLOSING;
this.socket.close(function () {
_this3.socketStatus = STATUS$2.IS_CLOSED;
_this3.notify('close');
_this3.websocket.close();
_this3.websocket.close(function () {
_this3.socketStatus = STATUS$2.IS_CLOSED;
_this3.notify('close');
});
});
}
}, {
key: 'send',
value: function send(binary) {
var customOptions = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
var options = mergeOptions(this.options, customOptions);
var options = mergeOptions$1(this.options, customOptions);
var receiver = options.receiver;
if (receiver === 'udp') {
var data = binary instanceof Buffer ? binary : new Buffer(binary);
Expand Down Expand Up @@ -1159,7 +1171,7 @@ var WebsocketClientPlugin = function () {
return WebsocketClientPlugin;
}();

var WebsocketServer = typeof __dirname !== 'undefined' ? require('ws').Server : undefined;
var WebsocketServer$1 = typeof __dirname !== 'undefined' ? require('ws').Server : undefined;
var STATUS$4 = {
IS_NOT_INITIALIZED: -1,
IS_CONNECTING: 0,
Expand All @@ -1173,7 +1185,7 @@ var STATUS$4 = {
var WebsocketServerPlugin = function () {
function WebsocketServerPlugin(customOptions) {
classCallCheck(this, WebsocketServerPlugin);
if (!WebsocketServer) {
if (!WebsocketServer$1) {
throw new Error('WebsocketServerPlugin can not be used in browser context');
}
this.options = Object.assign({}, defaultOptions$5, customOptions);
Expand Down Expand Up @@ -1202,7 +1214,7 @@ var WebsocketServerPlugin = function () {
if (this.socket) {
this.close();
}
this.socket = new WebsocketServer({ host: host, port: port });
this.socket = new WebsocketServer$1({ host: host, port: port });
this.socket.binaryType = 'arraybuffer';
this.socketStatus = STATUS$4.IS_CONNECTING;
this.socket.on('listening', function () {
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 15 additions & 9 deletions src/plugin/bridge.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const dgram = typeof __dirname !== 'undefined' ? require('dgram') : undefined
const WebSocket = typeof __dirname !== 'undefined' ? require('ws').Server : undefined
const WebsocketServer = typeof __dirname !== 'undefined' ? require('ws').Server : undefined

/**
* Status flags
Expand Down Expand Up @@ -71,7 +71,7 @@ export default class BridgePlugin {
* const osc = new OSC({ plugin: plugin })
*/
constructor(customOptions = {}) {
if (!dgram || !WebSocket) {
if (!dgram || !WebsocketServer) {
throw new Error('BridgePlugin can not be used in browser context')
}

Expand Down Expand Up @@ -151,10 +151,18 @@ export default class BridgePlugin {
exclusive: options.udpServer.exclusive,
}, () => {
// bind Websocket server
this.websocket = new WebSocket({ host: options.wsServer.host, port: options.wsServer.port })
this.websocket = new WebsocketServer({
host: options.wsServer.host,
port: options.wsServer.port,
})
this.websocket.binaryType = 'arraybuffer'

// register Websocket events
this.websocket.on('listening', () => {
this.socketStatus = STATUS.IS_OPEN
this.notify('open')
})

this.websocket.on('error', (error) => {
this.notify('error', error)
})
Expand All @@ -165,8 +173,6 @@ export default class BridgePlugin {
this.notify(new Uint8Array(message))
})
})

this.notify('open')
})
}

Expand All @@ -178,11 +184,11 @@ export default class BridgePlugin {

// close udp socket
this.socket.close(() => {
this.socketStatus = STATUS.IS_CLOSED
this.notify('close')

// close Websocket
this.websocket.close()
this.websocket.close(() => {
this.socketStatus = STATUS.IS_CLOSED
this.notify('close')
})
})
}

Expand Down
Loading

0 comments on commit 2b0c7ae

Please sign in to comment.