Skip to content

Commit

Permalink
Streamline websocket selection (crossbario#479)
Browse files Browse the repository at this point in the history
* update deprecated packages; fixes 469

* if globals doesn't have WebSocket, use external ws
  • Loading branch information
om26er authored and oberstet committed Oct 18, 2019
1 parent c380038 commit d478577
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 89 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@
"eth-sig-util": "^2.4.4",
"msgpack5": ">= 3.6.0",
"sha3": ">=2.0.1",
"truffle-contract": ">=4.0.10",
"@truffle/contract": ">=4.0.36",
"tweetnacl": ">=0.14.3",
"tweetnacl-sealedbox-js": ">=1.1.0",
"uuid": ">=3.3.2",
"uuid-parse": ">=1.1.0",
"web3": ">=1.0.0-beta.53",
"web3-eth-accounts": "^1.2.1",
"web3-utils": "^1.2.1",
Expand Down
2 changes: 1 addition & 1 deletion packages/autobahn-xbr/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"tweetnacl": ">= 0.14.3",
"tweetnacl-sealedbox-js": ">=1.1.0",
"web3": ">=1.2.1",
"truffle-contract": ">=4.0.10"
"@truffle/contract": ">=4.0.36"
},
"browser": {
"ws": false
Expand Down
145 changes: 59 additions & 86 deletions packages/autobahn/lib/transport/websocket.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,15 +93,65 @@ Factory.prototype.create = function () {
protocol: null
};

if ("WebSocket" in global) {
(function () {

var websocket;

if (self._options.protocols) {
websocket = new global.WebSocket(self._options.url, self._options.protocols);
} else {
websocket = new global.WebSocket(self._options.url);
}
websocket.binaryType = 'arraybuffer';

websocket.onmessage = function (evt) {
log.debug("WebSocket transport receive", evt.data);

var msg = transport.serializer.unserialize(evt.data);
transport.onmessage(msg);
}

websocket.onopen = function () {
var serializer_part = websocket.protocol.split('.')[2];
for (var index in self._options.serializers) {
var serializer = self._options.serializers[index];
if (serializer.SERIALIZER_ID == serializer_part) {
transport.serializer = serializer;
break;
}
}

transport.info.protocol = websocket.protocol;
transport.onopen();
}

websocket.onclose = function (evt) {
var details = {
code: evt.code,
reason: evt.message,
wasClean: evt.wasClean
}
transport.onclose(details);
}

// Test below used to be via the 'window' object in the browser.
// This fails when running in a Web worker.
//
// running in Node.js
//
if (global.process && global.process.versions.node
&& !global.process.versions.hasOwnProperty('electron') && !global.process.__nwjs) {
// do NOT do the following, since that will make
// transport.onclose() fire twice (browsers already fire
// websocket.onclose() for errors also)
//websocket.onerror = websocket.onclose;

transport.send = function (msg) {
var payload = transport.serializer.serialize(msg);
log.debug("WebSocket transport send", payload);
websocket.send(payload);
}

transport.close = function (code, reason) {
websocket.close(code, reason);
};

})();
} else {
(function () {

var WebSocket = require('ws'); // https://github.com/websockets/ws
Expand Down Expand Up @@ -144,7 +194,7 @@ Factory.prototype.create = function () {
"'ca' 'cert' and 'key' parameters.");
}
} else {
log.debug('Not using TLS Client Authentication.');
log.debug('Not using TLS Client Authentication.');
}

websocket = new WebSocket(self._options.url, protocols, options);
Expand Down Expand Up @@ -216,7 +266,7 @@ Factory.prototype.create = function () {
// https://developer.mozilla.org/en-US/docs/Web/API/CloseEvent#Close_codes
//
websocket.on('close', function (code, message) {
if (auto_ping_interval != null) clearInterval(auto_ping_interval);
if (auto_ping_interval != null) clearInterval(auto_ping_interval);
var details = {
code: code,
reason: message,
Expand All @@ -235,83 +285,6 @@ Factory.prototype.create = function () {
transport.onclose(details);
});

})();
//
// running in the browser
//
} else {

(function () {

var websocket;

// Chrome, MSIE, newer Firefox
if ("WebSocket" in global) {

if (self._options.protocols) {
websocket = new global.WebSocket(self._options.url, self._options.protocols);
} else {
websocket = new global.WebSocket(self._options.url);
}
websocket.binaryType = 'arraybuffer';

// older versions of Firefox prefix the WebSocket object
} else if ("MozWebSocket" in global) {

if (self._options.protocols) {
websocket = new global.MozWebSocket(self._options.url, self._options.protocols);
} else {
websocket = new global.MozWebSocket(self._options.url);
}
} else {
throw "browser does not support WebSocket or WebSocket in Web workers";
}

websocket.onmessage = function (evt) {
log.debug("WebSocket transport receive", evt.data);

var msg = transport.serializer.unserialize(evt.data);
transport.onmessage(msg);
}

websocket.onopen = function () {
var serializer_part = websocket.protocol.split('.')[2];
for (var index in self._options.serializers) {
var serializer = self._options.serializers[index];
if (serializer.SERIALIZER_ID == serializer_part) {
transport.serializer = serializer;
break;
}
}

transport.info.protocol = websocket.protocol;
transport.onopen();
}

websocket.onclose = function (evt) {
var details = {
code: evt.code,
reason: evt.message,
wasClean: evt.wasClean
}
transport.onclose(details);
}

// do NOT do the following, since that will make
// transport.onclose() fire twice (browsers already fire
// websocket.onclose() for errors also)
//websocket.onerror = websocket.onclose;

transport.send = function (msg) {
var payload = transport.serializer.serialize(msg);
log.debug("WebSocket transport send", payload);
websocket.send(payload);
}

transport.close = function (code, reason) {
websocket.close(code, reason);
};

})();
}

Expand Down
2 changes: 1 addition & 1 deletion packages/autobahn/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"/lib"
],
"engines": {
"node": ">= 4.2.6"
"node": ">= 6.17.1"
},
"dependencies": {
"cbor": ">= 3.0.0",
Expand Down

0 comments on commit d478577

Please sign in to comment.