Skip to content

Commit

Permalink
Merge branch 'gh-58'
Browse files Browse the repository at this point in the history
* gh-58:
  Fixes gh-58: Fires error events when sending on a closed port. Factors WebSocketPort into separate cross-platform file.
  • Loading branch information
colinbdclark committed May 25, 2016
2 parents 25672ad + 46b2f74 commit 7ad1d47
Show file tree
Hide file tree
Showing 14 changed files with 112 additions and 120 deletions.
2 changes: 1 addition & 1 deletion Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ module.exports = function(grunt) {

oscWeb: [
"src/osc-transports.js",
"src/platforms/osc-browser.js"
"src/platforms/osc-websocket-client.js"
],

oscChrome: [
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ oscPort.on("message", function (oscMsg) {

##### Sending OSC messages:
```javascript
// For most Ports, send() should only be called after the "open" event fires.
oscPort.send({
address: "/carrier/frequency",
args: 440
Expand Down
31 changes: 23 additions & 8 deletions dist/osc-browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -3001,6 +3001,12 @@ var osc = osc || require("./osc.js"),
}
};

osc.fireClosedPortSendError = function (port, msg) {
msg = msg || "Can't send packets on a closed osc.Port object. Please open (or reopen) this Port by calling open().";

port.emit("error", msg);
};

osc.Port = function (options) {
this.options = options || {};
this.on("data", this.decodeOSC.bind(this));
Expand Down Expand Up @@ -3182,27 +3188,30 @@ var osc = osc || require("./osc.js"),
;/*
* osc.js: An Open Sound Control library for JavaScript that works in both the browser and Node.js
*
* Browser transports for osc.js
* Cross-Platform Web Socket client transport for osc.js.
*
* Copyright 2014-2015, Colin Clark
* Copyright 2014-2016, Colin Clark
* Licensed under the MIT and GPL 3 licenses.
*/

/*global WebSocket*/
/*global WebSocket, require*/

var osc = osc;
var osc = osc || require("../osc.js");

(function () {

"use strict";

osc.WebSocket = typeof WebSocket !== "undefined" ? WebSocket : require ("ws");

osc.WebSocketPort = function (options) {
osc.Port.call(this, options);
this.on("open", this.listen.bind(this));

this.socket = options.socket;
if (this.socket) {
if (this.socket.readyState === 1) {
osc.WebSocketPort.setupSocketForBinary(this.socket);
this.emit("open", this.socket);
} else {
this.open();
Expand All @@ -3214,11 +3223,11 @@ var osc = osc;
p.constructor = osc.WebSocketPort;

p.open = function () {
if (!this.socket) {
this.socket = new WebSocket(this.options.url);
if (!this.socket || this.socket.readyState > 1) {
this.socket = new osc.WebSocket(this.options.url);
}

this.socket.binaryType = "arraybuffer";
osc.WebSocketPort.setupSocketForBinary(this.socket);

var that = this;
this.socket.onopen = function () {
Expand All @@ -3244,14 +3253,20 @@ var osc = osc;
};

p.sendRaw = function (encoded) {
if (!this.socket) {
if (!this.socket || this.socket.readyState !== 1) {
osc.fireClosedPortSendError(this);
return;
}

this.socket.send(encoded);
};

p.close = function (code, reason) {
this.socket.close(code, reason);
};

osc.WebSocketPort.setupSocketForBinary = function (socket) {
socket.binaryType = osc.isNode ? "nodebuffer" : "arraybuffer";
};

}());
2 changes: 1 addition & 1 deletion dist/osc-browser.min.js

Large diffs are not rendered by default.

33 changes: 25 additions & 8 deletions dist/osc-chromeapp.js
Original file line number Diff line number Diff line change
Expand Up @@ -3001,6 +3001,12 @@ var osc = osc || require("./osc.js"),
}
};

osc.fireClosedPortSendError = function (port, msg) {
msg = msg || "Can't send packets on a closed osc.Port object. Please open (or reopen) this Port by calling open().";

port.emit("error", msg);
};

osc.Port = function (options) {
this.options = options || {};
this.on("data", this.decodeOSC.bind(this));
Expand Down Expand Up @@ -3182,27 +3188,30 @@ var osc = osc || require("./osc.js"),
;/*
* osc.js: An Open Sound Control library for JavaScript that works in both the browser and Node.js
*
* Browser transports for osc.js
* Cross-Platform Web Socket client transport for osc.js.
*
* Copyright 2014-2015, Colin Clark
* Copyright 2014-2016, Colin Clark
* Licensed under the MIT and GPL 3 licenses.
*/

/*global WebSocket*/
/*global WebSocket, require*/

var osc = osc;
var osc = osc || require("../osc.js");

(function () {

"use strict";

osc.WebSocket = typeof WebSocket !== "undefined" ? WebSocket : require ("ws");

osc.WebSocketPort = function (options) {
osc.Port.call(this, options);
this.on("open", this.listen.bind(this));

this.socket = options.socket;
if (this.socket) {
if (this.socket.readyState === 1) {
osc.WebSocketPort.setupSocketForBinary(this.socket);
this.emit("open", this.socket);
} else {
this.open();
Expand All @@ -3214,11 +3223,11 @@ var osc = osc;
p.constructor = osc.WebSocketPort;

p.open = function () {
if (!this.socket) {
this.socket = new WebSocket(this.options.url);
if (!this.socket || this.socket.readyState > 1) {
this.socket = new osc.WebSocket(this.options.url);
}

this.socket.binaryType = "arraybuffer";
osc.WebSocketPort.setupSocketForBinary(this.socket);

var that = this;
this.socket.onopen = function () {
Expand All @@ -3244,16 +3253,22 @@ var osc = osc;
};

p.sendRaw = function (encoded) {
if (!this.socket) {
if (!this.socket || this.socket.readyState !== 1) {
osc.fireClosedPortSendError(this);
return;
}

this.socket.send(encoded);
};

p.close = function (code, reason) {
this.socket.close(code, reason);
};

osc.WebSocketPort.setupSocketForBinary = function (socket) {
socket.binaryType = osc.isNode ? "nodebuffer" : "arraybuffer";
};

}());
;/*
* osc.js: An Open Sound Control library for JavaScript that works in both the browser and Node.js
Expand Down Expand Up @@ -3323,6 +3338,7 @@ var osc = osc || {};

p.sendRaw = function (encoded) {
if (!this.connectionId) {
osc.fireClosedPortSendError(this);
return;
}

Expand Down Expand Up @@ -3443,6 +3459,7 @@ var osc = osc || {};

p.sendRaw = function (encoded, address, port) {
if (!this.socketId) {
osc.fireClosedPortSendError(this);
return;
}

Expand Down
2 changes: 1 addition & 1 deletion dist/osc-chromeapp.min.js

Large diffs are not rendered by default.

31 changes: 23 additions & 8 deletions dist/osc-module.js
Original file line number Diff line number Diff line change
Expand Up @@ -1117,6 +1117,12 @@ var osc = osc || require("./osc.js"),
}
};

osc.fireClosedPortSendError = function (port, msg) {
msg = msg || "Can't send packets on a closed osc.Port object. Please open (or reopen) this Port by calling open().";

port.emit("error", msg);
};

osc.Port = function (options) {
this.options = options || {};
this.on("data", this.decodeOSC.bind(this));
Expand Down Expand Up @@ -1298,27 +1304,30 @@ var osc = osc || require("./osc.js"),
;/*
* osc.js: An Open Sound Control library for JavaScript that works in both the browser and Node.js
*
* Browser transports for osc.js
* Cross-Platform Web Socket client transport for osc.js.
*
* Copyright 2014-2015, Colin Clark
* Copyright 2014-2016, Colin Clark
* Licensed under the MIT and GPL 3 licenses.
*/

/*global WebSocket*/
/*global WebSocket, require*/

var osc = osc;
var osc = osc || require("../osc.js");

(function () {

"use strict";

osc.WebSocket = typeof WebSocket !== "undefined" ? WebSocket : require ("ws");

osc.WebSocketPort = function (options) {
osc.Port.call(this, options);
this.on("open", this.listen.bind(this));

this.socket = options.socket;
if (this.socket) {
if (this.socket.readyState === 1) {
osc.WebSocketPort.setupSocketForBinary(this.socket);
this.emit("open", this.socket);
} else {
this.open();
Expand All @@ -1330,11 +1339,11 @@ var osc = osc;
p.constructor = osc.WebSocketPort;

p.open = function () {
if (!this.socket) {
this.socket = new WebSocket(this.options.url);
if (!this.socket || this.socket.readyState > 1) {
this.socket = new osc.WebSocket(this.options.url);
}

this.socket.binaryType = "arraybuffer";
osc.WebSocketPort.setupSocketForBinary(this.socket);

var that = this;
this.socket.onopen = function () {
Expand All @@ -1360,16 +1369,22 @@ var osc = osc;
};

p.sendRaw = function (encoded) {
if (!this.socket) {
if (!this.socket || this.socket.readyState !== 1) {
osc.fireClosedPortSendError(this);
return;
}

this.socket.send(encoded);
};

p.close = function (code, reason) {
this.socket.close(code, reason);
};

osc.WebSocketPort.setupSocketForBinary = function (socket) {
socket.binaryType = osc.isNode ? "nodebuffer" : "arraybuffer";
};

}());
;
return osc;
Expand Down
Loading

0 comments on commit 7ad1d47

Please sign in to comment.