Skip to content
This repository has been archived by the owner on Mar 28, 2023. It is now read-only.

Websocket Error Syntax Fix plus Big Number fixes. #1908

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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
144 changes: 96 additions & 48 deletions js/utils/Socket.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,45 @@
import _ from 'underscore';
import { Events } from 'backbone';
'use strict';

Object.defineProperty(exports, "__esModule", {
value: true
});

var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();

var _underscore = require('underscore');

var _underscore2 = _interopRequireDefault(_underscore);

var _backbone = require('backbone');

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } }

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

/**
* Class that wraps the standard WebSocket class allowing us to add in
* some "sugar", which, as of now, consists mainly of mixing in
* Backbone.Events which gives us a more robust events than the
* barebones setup natively on the WebSocket.
*/
export default class {
var _class = function () {
/**
* Construct a new socket instance.
* @constructor
* @param {string} url - The websocket url.
*/
constructor(url, options = {}) {
function _class(url) {
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};

_classCallCheck(this, _class);

if (!url) {
throw new Error('Please provide an url.');
}

_.extend(this, Events);
_underscore2.default.extend(this, _backbone.Events);
this.url = url;
if (options.autoConnect) this.connect();
}
Expand All @@ -29,59 +50,86 @@ export default class {
* will directly call this is if your connection was lost and
* you want to reconnect to the same url.
*/
connect() {
if (this._socket && this._socket.readyState < 2) return;

if (this._socket) {
this._socket.onopen = null;
this._socket.onclose = null;
this._socket.onerror = null;
this._socket.onmessage = null;
}

this._socket = new WebSocket(this.url);
this._proxyEvent('onopen');
this._proxyEvent('onclose');
this._proxyEvent('onerror');

this._socket.onmessage = (...args) => {
if (args[0] && args[0].data) {
args[0].jsonData = JSON.parse(args[0].data);
} else {
args[0].jsonData = {};
_createClass(_class, [{
key: 'connect',
value: function connect() {
var _this = this;

if (this._socket && this._socket.readyState < 2) return;

if (this._socket) {
this._socket.onopen = null;
this._socket.onclose = null;
this._socket.onerror = null;
this._socket.onmessage = null;
}

this.trigger(...['message'].concat(args));
};
}
this._socket = new WebSocket("this.url");
this._proxyEvent('onopen');
this._proxyEvent('onclose');
this._proxyEvent('onerror');

_proxyEvent(event) {
this._socket[event] = (...args) => {
this.trigger(...[event.slice(2)].concat(args));
};
}
this._socket.onmessage = function () {
for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}

if (args[0] && args[0].data) {
args[0].jsonData = JSON.parse(args[0].data);
} else {
args[0].jsonData = {};
}

close(...args) {
if (this._socket) {
this._socket.close(...args);
_this.trigger.apply(_this, _toConsumableArray(['message'].concat(args)));
};
}
}
}, {
key: '_proxyEvent',
value: function _proxyEvent(event) {
var _this2 = this;

this._socket[event] = function () {
for (var _len2 = arguments.length, args = Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
args[_key2] = arguments[_key2];
}

send(...args) {
if (!this._socket) {
throw new Error('The socket is not connected. Please connect() first ' +
'before sending.');
_this2.trigger.apply(_this2, _toConsumableArray([event.slice(2)].concat(args)));
};
}
}, {
key: 'close',
value: function close() {
if (this._socket) {
var _socket;

this._socket.send(...args);
}
(_socket = this._socket).close.apply(_socket, arguments);
}
}
}, {
key: 'send',
value: function send() {
var _socket2;

get readyState() {
if (!this._socket) {
throw new Error('There is no ready state because the socket has never had a connection ' +
'attempt. Please connect() first,');
if (!this._socket) {
throw new Error('The socket is not connected. Please connect() first ' + 'before sending.');
}

(_socket2 = this._socket).send.apply(_socket2, arguments);
}
}, {
key: 'readyState',
get: function get() {
if (!this._socket) {
throw new Error('There is no ready state because the socket has never had a connection ' + 'attempt. Please connect() first,');
}

return this._socket.readyState;
}
}
return this._socket.readyState;
}
}]);

return _class;
}();

exports.default = _class;