websocket.js
Advanced tools
Comparing version 0.1.6 to 0.1.7
@@ -1,20 +0,20 @@ | ||
'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;};}();function _classCallCheck(instance,Constructor){if(!(instance instanceof Constructor)){throw new TypeError("Cannot call a class as a function");}}var backoff=require('backoff');var WebSocketClient=function(){ /** | ||
'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;};}();function _classCallCheck(instance,Constructor){if(!(instance instanceof Constructor)){throw new TypeError("Cannot call a class as a function");}}var backoff=require('backoff');var WebSocketClient=function(){/** | ||
* @param url DOMString The URL to which to connect; this should be the URL to which the WebSocket server will respond. | ||
* @param protocols DOMString|DOMString[] Either a single protocol string or an array of protocol strings. These strings are used to indicate sub-protocols, so that a single server can implement multiple WebSocket sub-protocols (for example, you might want one server to be able to handle different types of interactions depending on the specified protocol). If you don't specify a protocol string, an empty string is assumed. | ||
*/function WebSocketClient(url,protocols){var options=arguments.length<=2||arguments[2]===undefined?{}:arguments[2];_classCallCheck(this,WebSocketClient);this.url=url;this._protocols=protocols;this.reconnectEnabled=true;this.listeners={};this.backoff=backoff[options.backoff||'fibonacci'](options);this.backoff.on('backoff',this.onBackoffStart.bind(this));this.backoff.on('ready',this.onBackoffReady.bind(this));this.backoff.on('fail',this.onBackoffFail.bind(this));this.open();}_createClass(WebSocketClient,[{key:'open',value:function open(){var reconnect=arguments.length<=0||arguments[0]===undefined?false:arguments[0];this.isReconnect=reconnect;this.ws=new WebSocket(this.url,this._protocols);this.ws.onclose=this.onCloseCallback.bind(this);this.ws.onerror=this.onErrorCallback.bind(this);this.ws.onmessage=this.onMessageCallback.bind(this);this.ws.onopen=this.onOpenCallback.bind(this);} /** | ||
*/function WebSocketClient(url,protocols){var options=arguments.length>2&&arguments[2]!==undefined?arguments[2]:{};_classCallCheck(this,WebSocketClient);this.url=url;this._protocols=protocols;this.reconnectEnabled=true;this.listeners={};this.backoff=backoff[options.backoff||'fibonacci'](options);this.backoff.on('backoff',this.onBackoffStart.bind(this));this.backoff.on('ready',this.onBackoffReady.bind(this));this.backoff.on('fail',this.onBackoffFail.bind(this));this.open();}_createClass(WebSocketClient,[{key:'open',value:function open(){var reconnect=arguments.length>0&&arguments[0]!==undefined?arguments[0]:false;this.isReconnect=reconnect;this.ws=new WebSocket(this.url,this._protocols);this.ws.onclose=this.onCloseCallback.bind(this);this.ws.onerror=this.onErrorCallback.bind(this);this.ws.onmessage=this.onMessageCallback.bind(this);this.ws.onopen=this.onOpenCallback.bind(this);}/** | ||
* @ignore | ||
*/},{key:'onBackoffStart',value:function onBackoffStart(number,delay){} /** | ||
*/},{key:'onBackoffStart',value:function onBackoffStart(number,delay){}/** | ||
* @ignore | ||
*/},{key:'onBackoffReady',value:function onBackoffReady(number,delay){ // console.log("onBackoffReady", number + ' ' + delay + 'ms'); | ||
this.open(true);} /** | ||
*/},{key:'onBackoffReady',value:function onBackoffReady(number,delay){// console.log("onBackoffReady", number + ' ' + delay + 'ms'); | ||
this.open(true);}/** | ||
* @ignore | ||
*/},{key:'onBackoffFail',value:function onBackoffFail(){} /** | ||
*/},{key:'onBackoffFail',value:function onBackoffFail(){}/** | ||
* @ignore | ||
*/},{key:'onCloseCallback',value:function onCloseCallback(){if(!this.isReconnect&&this.listeners['onclose'])this.listeners['onclose'].apply(null,arguments);if(this.reconnectEnabled){this.backoff.backoff();}} /** | ||
*/},{key:'onCloseCallback',value:function onCloseCallback(){if(!this.isReconnect&&this.listeners['onclose'])this.listeners['onclose'].apply(null,arguments);if(this.reconnectEnabled){this.backoff.backoff();}}/** | ||
* @ignore | ||
*/},{key:'onErrorCallback',value:function onErrorCallback(){if(this.listeners['onerror'])this.listeners['onerror'].apply(null,arguments);} /** | ||
*/},{key:'onErrorCallback',value:function onErrorCallback(){if(this.listeners['onerror'])this.listeners['onerror'].apply(null,arguments);}/** | ||
* @ignore | ||
*/},{key:'onMessageCallback',value:function onMessageCallback(){if(this.listeners['onmessage'])this.listeners['onmessage'].apply(null,arguments);} /** | ||
*/},{key:'onMessageCallback',value:function onMessageCallback(){if(this.listeners['onmessage'])this.listeners['onmessage'].apply(null,arguments);}/** | ||
* @ignore | ||
*/},{key:'onOpenCallback',value:function onOpenCallback(){if(this.listeners['onopen'])this.listeners['onopen'].apply(null,arguments);if(this.isReconnect&&this.listeners['onreconnect'])this.listeners['onreconnect'].apply(null,arguments);this.isReconnect=false;} /** | ||
*/},{key:'onOpenCallback',value:function onOpenCallback(){if(this.listeners['onopen'])this.listeners['onopen'].apply(null,arguments);if(this.isReconnect&&this.listeners['onreconnect'])this.listeners['onreconnect'].apply(null,arguments);this.isReconnect=false;}/** | ||
* The number of bytes of data that have been queued using calls to send() | ||
@@ -27,3 +27,3 @@ * but not yet transmitted to the network. This value does not reset to zero | ||
* @readonly | ||
*/},{key:'close', /** | ||
*/},{key:'close',/** | ||
* Closes the WebSocket connection or connection attempt, if any. If the | ||
@@ -36,14 +36,14 @@ * connection is already CLOSED, this method does nothing. | ||
* @return void | ||
*/value:function close(code,reason){if(typeof code=='undefined'){code=1000;}this.reconnectEnabled=false;this.ws.close(code,reason);} /** | ||
*/value:function close(code,reason){if(typeof code=='undefined'){code=1000;}this.reconnectEnabled=false;this.ws.close(code,reason);}/** | ||
* Transmits data to the server over the WebSocket connection. | ||
* @param data DOMString|ArrayBuffer|Blob | ||
* @return void | ||
*/},{key:'send',value:function send(data){this.ws.send(data);} /** | ||
*/},{key:'send',value:function send(data){this.ws.send(data);}/** | ||
* An event listener to be called when the WebSocket connection's readyState changes to CLOSED. The listener receives a CloseEvent named "close". | ||
* @param listener EventListener | ||
*/},{key:'bufferedAmount',get:function get(){return this.ws.bufferedAmount;} /** | ||
*/},{key:'bufferedAmount',get:function get(){return this.ws.bufferedAmount;}/** | ||
* The current state of the connection; this is one of the Ready state constants. | ||
* @type unsigned short | ||
* @readonly | ||
*/},{key:'readyState',get:function get(){return this.ws.readyState;} /** | ||
*/},{key:'readyState',get:function get(){return this.ws.readyState;}/** | ||
* A string indicating the type of binary data being transmitted by the | ||
@@ -53,7 +53,7 @@ * connection. This should be either "blob" if DOM Blob objects are being | ||
* @type DOMString | ||
*/},{key:'binaryType',get:function get(){return this.ws.binaryType;},set:function set(binaryType){this.ws.binaryType=binaryType;} /** | ||
*/},{key:'binaryType',get:function get(){return this.ws.binaryType;},set:function set(binaryType){this.ws.binaryType=binaryType;}/** | ||
* The extensions selected by the server. This is currently only the empty | ||
* string or a list of extensions as negotiated by the connection. | ||
* @type DOMString | ||
*/},{key:'extensions',get:function get(){return this.ws.extensions;},set:function set(extensions){this.ws.extensions=extensions;} /** | ||
*/},{key:'extensions',get:function get(){return this.ws.extensions;},set:function set(extensions){this.ws.extensions=extensions;}/** | ||
* A string indicating the name of the sub-protocol the server selected; | ||
@@ -63,21 +63,21 @@ * this will be one of the strings specified in the protocols parameter when | ||
* @type DOMString | ||
*/},{key:'protocol',get:function get(){return this.ws.protocol;},set:function set(protocol){this.ws.protocol=protocol;}},{key:'onclose',set:function set(listener){this.listeners['onclose']=listener;},get:function get(){return this.listeners['onclose'];} /** | ||
*/},{key:'protocol',get:function get(){return this.ws.protocol;},set:function set(protocol){this.ws.protocol=protocol;}},{key:'onclose',set:function set(listener){this.listeners['onclose']=listener;},get:function get(){return this.listeners['onclose'];}/** | ||
* An event listener to be called when an error occurs. This is a simple event named "error". | ||
* @param listener EventListener | ||
*/},{key:'onerror',set:function set(listener){this.listeners['onerror']=listener;},get:function get(){return this.listeners['onerror'];} /** | ||
*/},{key:'onerror',set:function set(listener){this.listeners['onerror']=listener;},get:function get(){return this.listeners['onerror'];}/** | ||
* An event listener to be called when a message is received from the server. The listener receives a MessageEvent named "message". | ||
* @param listener EventListener | ||
*/},{key:'onmessage',set:function set(listener){this.listeners['onmessage']=listener;},get:function get(){return this.listeners['onmessage'];} /** | ||
*/},{key:'onmessage',set:function set(listener){this.listeners['onmessage']=listener;},get:function get(){return this.listeners['onmessage'];}/** | ||
* An event listener to be called when the WebSocket connection's readyState changes to OPEN; this indicates that the connection is ready to send and receive data. The event is a simple one with the name "open". | ||
* @param listener EventListener | ||
*/},{key:'onopen',set:function set(listener){this.listeners['onopen']=listener;},get:function get(){return this.listeners['onopen'];} /** | ||
*/},{key:'onopen',set:function set(listener){this.listeners['onopen']=listener;},get:function get(){return this.listeners['onopen'];}/** | ||
* @param listener EventListener | ||
*/},{key:'onreconnect',set:function set(listener){this.listeners['onreconnect']=listener;},get:function get(){return this.listeners['onreconnect'];}}]);return WebSocketClient;}(); /** | ||
*/},{key:'onreconnect',set:function set(listener){this.listeners['onreconnect']=listener;},get:function get(){return this.listeners['onreconnect'];}}]);return WebSocketClient;}();/** | ||
* The connection is not yet open. | ||
*/WebSocketClient.CONNECTING=WebSocket.CONNECTING; /** | ||
*/WebSocketClient.CONNECTING=WebSocket.CONNECTING;/** | ||
* The connection is open and ready to communicate. | ||
*/WebSocketClient.OPEN=WebSocket.OPEN; /** | ||
*/WebSocketClient.OPEN=WebSocket.OPEN;/** | ||
* The connection is in the process of closing. | ||
*/WebSocketClient.CLOSING=WebSocket.CLOSING; /** | ||
*/WebSocketClient.CLOSING=WebSocket.CLOSING;/** | ||
* The connection is closed or couldn't be opened. | ||
*/WebSocketClient.CLOSED=WebSocket.CLOSED;exports.default=WebSocketClient; |
{ | ||
"name": "websocket.js", | ||
"version": "0.1.6", | ||
"version": "0.1.7", | ||
"description": "WebSocket browser client with reconnect back-off feature.", | ||
@@ -10,2 +10,3 @@ "repository": { | ||
"main": "lib/index.js", | ||
"types": "types/index.d.ts", | ||
"scripts": { | ||
@@ -22,7 +23,5 @@ "start": "npm run prepublish & nodemon ./examples/server.js", | ||
"babel-preset-es2015": "^6.1.2", | ||
"browserify": "^12.0.1", | ||
"babelify": "^7.2.0", | ||
"uglify-js": "^2.6.1", | ||
"express": "^4.13.3", | ||
@@ -29,0 +28,0 @@ "nodemon": "^1.7.1", |
119654
15
2316