sockstream
Advanced tools
Comparing version 1.4.0 to 1.4.1
@@ -36,3 +36,3 @@ 'use strict'; | ||
var clientName = 'sockstream.js'; | ||
var clientVersion = '1.3.3'; | ||
var clientVersion = '1.4.0'; | ||
var defaultKeepAlive = 800; | ||
@@ -94,3 +94,3 @@ var defaultDebugLevel = 0; | ||
this.on('ping', function (message) { | ||
_this.send('pong'); | ||
_this.emit('pong'); | ||
_this.console(['[PING]', message], 'debug'); | ||
@@ -240,3 +240,3 @@ }); | ||
setTimeout(function () { | ||
_this2.send('connecting', cb); | ||
_this2.emit('connecting', cb); | ||
}, 20); | ||
@@ -277,3 +277,3 @@ }; | ||
_this2.console('Attempting to reestablish WebSocket', 'info'); | ||
_this2.open(); | ||
_this2.connect(cb, connection); | ||
}, timeout); | ||
@@ -280,0 +280,0 @@ } |
@@ -1,1 +0,1 @@ | ||
"use strict";function _toConsumableArray(e){if(Array.isArray(e)){for(var t=0,n=Array(e.length);t<e.length;t++)n[t]=e[t];return n}return Array.from(e)}function _classCallCheck(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}Object.defineProperty(exports,"__esModule",{value:!0});var _typeof="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e},_createClass=function(){function e(e,t){for(var n=0;n<t.length;n++){var o=t[n];o.enumerable=o.enumerable||!1,o.configurable=!0,"value"in o&&(o.writable=!0),Object.defineProperty(e,o.key,o)}}return function(t,n,o){return n&&e(t.prototype,n),o&&e(t,o),t}}(),StreamSock=function(){function e(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:"localhost",n=arguments.length>1&&void 0!==arguments[1]?arguments[1]:8082,o=!(arguments.length>2&&void 0!==arguments[2])||arguments[2],i=this,s=arguments.length>3&&void 0!==arguments[3]?arguments[3]:800,c=arguments.length>4&&void 0!==arguments[4]?arguments[4]:0;_classCallCheck(this,e);var r="sockstream.js",a="1.3.3",l=800,f=0,u=8082,p="wss://",h="127.0.0.1";this.callbackRegister={anonymous:{}},this.maxKeepAliveAttempt=20,this.keepAliveAttempt=0,this.disconnect=[],this.connections={},this._config={client:{name:r,version:a,debugLevel:f,keepAlive:l},server:{hostname:h,port:u,proto:p}},"object"===("undefined"==typeof t?"undefined":_typeof(t))?("object"===_typeof(t.client)&&(this._config.client=Object.assign({debugLevel:f,keepAlive:l},t.client,{name:r,version:a})),"object"===_typeof(t.server)&&(this._config.server=Object.assign({hostname:h,port:u,proto:p},t.server))):this._config={client:{name:r,version:a,debugLevel:c,keepAlive:s},server:{hostname:t,port:n,proto:o?"wss://":"ws://"}},this.on("ping",function(e){i.send("pong"),i.console(["[PING]",e],"debug")}),this.on("system",function(e){i.console(["[SYSTEM]",e],"info")})}return _createClass(e,[{key:"console",value:function(e){function t(t){return e.apply(this,arguments)}return t.toString=function(){return e.toString()},t}(function(e){var t,n=arguments.length>1&&void 0!==arguments[1]?arguments[1]:"log",o={log:4,debug:3,info:2,warn:1,error:0};this._config.client.debugLevel>=(o[n]||0)&&(t=console)[n].apply(t,_toConsumableArray(e))})},{key:"debug",value:function(e){return"undefined"!=typeof e&&(this._config.client.debug=!!e),this._config.client.debug}},{key:"close",value:function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:null,t=e||this.lastConnectionId;return"undefined"!=typeof this.connections[t]&&(this.disconnect.push(t),this.connections[t].close()),!0}},{key:"on",value:function(e,t){"function"==typeof t&&("undefined"==typeof this.callbackRegister[e]?this.callbackRegister[e]=[t]:Array.isArray(this.callbackRegister[e])&&this.callbackRegister[e].push(t))}},{key:"emit",value:function(t,n){var o=this.lastConnectionId;if("function"!=typeof n&&"undefined"!=typeof this.connections[n]&&(this.lastConnectionId=o=n),"undefined"==typeof this.connections[o])return!1;if(this.connections[o].readyState===this.connections[o].OPEN){var i=e.generateUUID();return"function"==typeof n&&(this.callbackRegister.anonymous[i]=n),this.connections[o].send(JSON.stringify({"@meta":Object.assign({_id:i},this._config),message:t})),!0}return this.console("WebSocket not open","warn"),!1}},{key:"connect",value:function(t,n){var o=this;n&&"string"==typeof n||(n=""+this._config.server.proto+this._config.server.hostname+":"+this._config.server.port),this.console(["[CONNECTING]",n],"info");var i=e.generateUUID();this.lastConnectionId=i;try{this.connections[i]=new WebSocket(""+this._config.server.proto+this._config.server.hostname+":"+this._config.server.port)}catch(e){return}return this.connections[i].readyState===this.connections[i].CONNECTING&&(this.connections[i].onopen=function(){o.console("connected","info"),o.keepAliveAttempt=0,setTimeout(function(){o.send("connecting",t)},20)},this.connections[i].onerror=function(e){o.console(e,"error")},this.connections[i].onmessage=function(t){var n=e.parseMessage(t.data);n&&"undefined"!=typeof n["@meta"]?("function"==typeof o.callbackRegister[n["@meta"]._type]?o.callbackRegister[n["@meta"]._type].forEach(function(e){return e.call(o._config,n.message)}):"function"==typeof o.callbackRegister.anonymous[n["@meta"]._id]&&(o.callbackRegister.anonymous[n["@meta"]._id].call(o._config,n.message),delete o.callbackRegister.anonymous[n["@meta"]._id]),o.console(["[RCVD]",n.message],"debug")):o.console(["[RCVD]",t.data],"debug")},this.connections[i].onclose=function(e){if(o.console(["WebSocket closed",e],"warn"),o.disconnect.indexOf(i)===-1&&"number"==typeof o._config.client.keepAlive&&o._config.client.keepAlive>0){if(++o.keepAliveAttempt>o.maxKeepAliveAttempt)return;var t=o._config.client.keepAlive;o.keepAliveAttempt>5?t+=t/2:o.keepAliveAttempt>10&&(t=2*t),setTimeout(function(){o.console("Attempting to reestablish WebSocket","info"),o.open()},t)}delete o.connections[i]}),i}},{key:"setMaxKeepAliveAttempt",value:function(e){return this.maxKeepAliveAttempt="number"==typeof e?e:this.maxKeepAliveAttempt,this}}],[{key:"generateUUID",value:function(){var e=(new Date).getTime();return window.performance&&"function"==typeof window.performance.now&&(e+=performance.now()),"xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g,function(t){var n=(e+16*Math.random())%16|0;return e=Math.floor(e/16),("x"==t?n:3&n|8).toString(16)})}},{key:"parseMessage",value:function(e){return"undefined"==typeof e?null:"string"==typeof e?JSON.parse(e):e}}]),e}();exports.default=StreamSock; | ||
"use strict";function _toConsumableArray(e){if(Array.isArray(e)){for(var t=0,n=Array(e.length);t<e.length;t++)n[t]=e[t];return n}return Array.from(e)}function _classCallCheck(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}Object.defineProperty(exports,"__esModule",{value:!0});var _typeof="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e},_createClass=function(){function e(e,t){for(var n=0;n<t.length;n++){var o=t[n];o.enumerable=o.enumerable||!1,o.configurable=!0,"value"in o&&(o.writable=!0),Object.defineProperty(e,o.key,o)}}return function(t,n,o){return n&&e(t.prototype,n),o&&e(t,o),t}}(),StreamSock=function(){function e(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:"localhost",n=arguments.length>1&&void 0!==arguments[1]?arguments[1]:8082,o=!(arguments.length>2&&void 0!==arguments[2])||arguments[2],i=this,s=arguments.length>3&&void 0!==arguments[3]?arguments[3]:800,c=arguments.length>4&&void 0!==arguments[4]?arguments[4]:0;_classCallCheck(this,e);var r="sockstream.js",a="1.4.0",l=800,f=0,u=8082,p="wss://",h="127.0.0.1";this.callbackRegister={anonymous:{}},this.maxKeepAliveAttempt=20,this.keepAliveAttempt=0,this.disconnect=[],this.connections={},this._config={client:{name:r,version:a,debugLevel:f,keepAlive:l},server:{hostname:h,port:u,proto:p}},"object"===("undefined"==typeof t?"undefined":_typeof(t))?("object"===_typeof(t.client)&&(this._config.client=Object.assign({debugLevel:f,keepAlive:l},t.client,{name:r,version:a})),"object"===_typeof(t.server)&&(this._config.server=Object.assign({hostname:h,port:u,proto:p},t.server))):this._config={client:{name:r,version:a,debugLevel:c,keepAlive:s},server:{hostname:t,port:n,proto:o?"wss://":"ws://"}},this.on("ping",function(e){i.emit("pong"),i.console(["[PING]",e],"debug")}),this.on("system",function(e){i.console(["[SYSTEM]",e],"info")})}return _createClass(e,[{key:"console",value:function(e){function t(t){return e.apply(this,arguments)}return t.toString=function(){return e.toString()},t}(function(e){var t,n=arguments.length>1&&void 0!==arguments[1]?arguments[1]:"log",o={log:4,debug:3,info:2,warn:1,error:0};this._config.client.debugLevel>=(o[n]||0)&&(t=console)[n].apply(t,_toConsumableArray(e))})},{key:"debug",value:function(e){return"undefined"!=typeof e&&(this._config.client.debug=!!e),this._config.client.debug}},{key:"close",value:function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:null,t=e||this.lastConnectionId;return"undefined"!=typeof this.connections[t]&&(this.disconnect.push(t),this.connections[t].close()),!0}},{key:"on",value:function(e,t){"function"==typeof t&&("undefined"==typeof this.callbackRegister[e]?this.callbackRegister[e]=[t]:Array.isArray(this.callbackRegister[e])&&this.callbackRegister[e].push(t))}},{key:"emit",value:function(t,n){var o=this.lastConnectionId;if("function"!=typeof n&&"undefined"!=typeof this.connections[n]&&(this.lastConnectionId=o=n),"undefined"==typeof this.connections[o])return!1;if(this.connections[o].readyState===this.connections[o].OPEN){var i=e.generateUUID();return"function"==typeof n&&(this.callbackRegister.anonymous[i]=n),this.connections[o].send(JSON.stringify({"@meta":Object.assign({_id:i},this._config),message:t})),!0}return this.console("WebSocket not open","warn"),!1}},{key:"connect",value:function(t,n){var o=this;n&&"string"==typeof n||(n=""+this._config.server.proto+this._config.server.hostname+":"+this._config.server.port),this.console(["[CONNECTING]",n],"info");var i=e.generateUUID();this.lastConnectionId=i;try{this.connections[i]=new WebSocket(""+this._config.server.proto+this._config.server.hostname+":"+this._config.server.port)}catch(e){return}return this.connections[i].readyState===this.connections[i].CONNECTING&&(this.connections[i].onopen=function(){o.console("connected","info"),o.keepAliveAttempt=0,setTimeout(function(){o.emit("connecting",t)},20)},this.connections[i].onerror=function(e){o.console(e,"error")},this.connections[i].onmessage=function(t){var n=e.parseMessage(t.data);n&&"undefined"!=typeof n["@meta"]?("function"==typeof o.callbackRegister[n["@meta"]._type]?o.callbackRegister[n["@meta"]._type].forEach(function(e){return e.call(o._config,n.message)}):"function"==typeof o.callbackRegister.anonymous[n["@meta"]._id]&&(o.callbackRegister.anonymous[n["@meta"]._id].call(o._config,n.message),delete o.callbackRegister.anonymous[n["@meta"]._id]),o.console(["[RCVD]",n.message],"debug")):o.console(["[RCVD]",t.data],"debug")},this.connections[i].onclose=function(e){if(o.console(["WebSocket closed",e],"warn"),o.disconnect.indexOf(i)===-1&&"number"==typeof o._config.client.keepAlive&&o._config.client.keepAlive>0){if(++o.keepAliveAttempt>o.maxKeepAliveAttempt)return;var s=o._config.client.keepAlive;o.keepAliveAttempt>5?s+=s/2:o.keepAliveAttempt>10&&(s=2*s),setTimeout(function(){o.console("Attempting to reestablish WebSocket","info"),o.connect(t,n)},s)}delete o.connections[i]}),i}},{key:"setMaxKeepAliveAttempt",value:function(e){return this.maxKeepAliveAttempt="number"==typeof e?e:this.maxKeepAliveAttempt,this}}],[{key:"generateUUID",value:function(){var e=(new Date).getTime();return window.performance&&"function"==typeof window.performance.now&&(e+=performance.now()),"xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g,function(t){var n=(e+16*Math.random())%16|0;return e=Math.floor(e/16),("x"==t?n:3&n|8).toString(16)})}},{key:"parseMessage",value:function(e){return"undefined"==typeof e?null:"string"==typeof e?JSON.parse(e):e}}]),e}();exports.default=StreamSock; |
{ | ||
"name": "sockstream", | ||
"version": "1.4.0", | ||
"version": "1.4.1", | ||
"description": "Provides a standard WebSocket thin manager", | ||
@@ -5,0 +5,0 @@ "main": "src/sockstream.js", |
@@ -68,3 +68,3 @@ export default class StreamSock { | ||
this.on('ping', message => { | ||
this.send('pong') | ||
this.emit('pong') | ||
this.console(['[PING]', message], 'debug') | ||
@@ -201,3 +201,3 @@ }) | ||
setTimeout(() => { | ||
this.send('connecting', cb) | ||
this.emit('connecting', cb) | ||
}, 20) | ||
@@ -236,3 +236,3 @@ } | ||
this.console('Attempting to reestablish WebSocket', 'info') | ||
this.open() | ||
this.connect(cb, connection) | ||
}, timeout) | ||
@@ -239,0 +239,0 @@ } |
39465