bridge.io-client
Advanced tools
Comparing version
@@ -21,3 +21,3 @@ /*! Bridge.IO-Client 2.x | ||
export default class BridgeIO extends EventEmitter { | ||
constructor(opt) { | ||
constructor(opt = {}) { | ||
super(); | ||
@@ -24,0 +24,0 @@ |
@@ -1,1 +0,1 @@ | ||
class EventEmitter{constructor(){this.events={}}on(t,e){this.events[t]||(this.events[t]=e)}emit(t,...e){this.events[t]&&this.events[t](...e)}}export default class BridgeIO extends EventEmitter{constructor(t){super(),this.opt=Object.assign({server:"ws://localhost",protocols:[],response_timeout:5e3,attempts:null,delay:2e3,reconnection:!0},t),this.socket=null,this.rpcId=0,this.responseCallbacks={},this.errorCallbacks={},this.rpcTimeouts={},this.reconnectionAttempts=0,this.reconnectionInterval=null,this.isReactNative="undefined"!=typeof navigator&&"string"==typeof navigator.product&&"reactnative"===navigator.product.toLowerCase(),this.connect()}connect(){(null===this.socket||this.socket.readyState>2)&&(this.socket=null,this.socket=new WebSocket(this.opt.server,this.opt.protocols),this.socket.binaryType="arraybuffer",this.socketEvents(),clearInterval(this.reconnectionInterval))}socketEvents(){this.socket.onopen=(t=>{const e=this.reconnectionAttempts>0;this.emit("open",t,e)}),this.socket.onerror=(t=>{this.emit("error",t)}),this.socket.onclose=(t=>{this.emit("disconnected",t),!t.wasClean&&this.opt.reconnection&&(this.reconnectionInterval=setInterval(()=>{this.reconnectionAttempts++,null===this.opt.attempts||this.reconnectionAttempts<=this.opt.attempts?(this.emit("reconnecting",t),this.connect()):clearInterval(this.reconnectionInterval)},this.opt.delay))}),this.socket.onmessage=(t=>{const{data:e}=t;if(this.isReadyFlag(e)){const e=this.reconnectionAttempts>0;this.emit("connection",t,e),e&&this.emit("reconnection",t),this.reconnectionAttempts=0}else if(this.isPong(e))this.ping();else try{this.message(e)}catch(t){console.warn(t)}})}message(t){t=JSON.parse(t);const[e,s,i]=t;if("string"==typeof e)switch(e){case"rpc":this.rpc(t);break;default:this.emit(e,s)}}rpc(t){const[e,s,i]=t;"[object Object]"===Object.prototype.toString.call(s)&&s.hasOwnProperty("status")&&Number.isInteger(s.status)&&s.status>=400?this.errorCallbacks[i]&&this.errorCallbacks[i](s):this.responseCallbacks[i]&&this.responseCallbacks[i](s),this.responseCallbacks[i]&&delete this.responseCallbacks[i],this.errorCallbacks[i]&&delete this.errorCallbacks[i],clearTimeout(this.rpcTimeouts[i])}serializeMessage(t,e,s){let i=[t,e,s];return i=JSON.stringify(i)}cast(t,e=null,s=null,i=null,n={}){let o;n=Object.assign({timeout:this.opt.response_timeout},n),s&&(o=++this.rpcId,this.responseCallbacks[o]=s,this.errorCallbacks[o]=i,this.rpcTimeouts[o]=setTimeout(()=>{delete this.responseCallbacks[o],this.errorCallbacks[o]&&(this.errorCallbacks[o](null),delete this.errorCallbacks[o])},n.timeout));const r=this.serializeMessage(t,e,o);this.socket.send(r)}isReadyFlag(t){return 11===Number(t)}isPong(t){return 10===Number(t)}ping(){this.socket.send(this.isReactNative?"9":9)}} | ||
class EventEmitter{constructor(){this.events={}}on(t,e){this.events[t]||(this.events[t]=e)}emit(t,...e){this.events[t]&&this.events[t](...e)}}export default class BridgeIO extends EventEmitter{constructor(t={}){super(),this.opt=Object.assign({server:"ws://localhost",protocols:[],response_timeout:5e3,attempts:null,delay:2e3,reconnection:!0},t),this.socket=null,this.rpcId=0,this.responseCallbacks={},this.errorCallbacks={},this.rpcTimeouts={},this.reconnectionAttempts=0,this.reconnectionInterval=null,this.isReactNative="undefined"!=typeof navigator&&"string"==typeof navigator.product&&"reactnative"===navigator.product.toLowerCase(),this.connect()}connect(){(null===this.socket||this.socket.readyState>2)&&(this.socket=null,this.socket=new WebSocket(this.opt.server,this.opt.protocols),this.socket.binaryType="arraybuffer",this.socketEvents(),clearInterval(this.reconnectionInterval))}socketEvents(){this.socket.onopen=(t=>{const e=this.reconnectionAttempts>0;this.emit("open",t,e)}),this.socket.onerror=(t=>{this.emit("error",t)}),this.socket.onclose=(t=>{this.emit("disconnected",t),!t.wasClean&&this.opt.reconnection&&(this.reconnectionInterval=setInterval(()=>{this.reconnectionAttempts++,null===this.opt.attempts||this.reconnectionAttempts<=this.opt.attempts?(this.emit("reconnecting",t),this.connect()):clearInterval(this.reconnectionInterval)},this.opt.delay))}),this.socket.onmessage=(t=>{const{data:e}=t;if(this.isReadyFlag(e)){const e=this.reconnectionAttempts>0;this.emit("connection",t,e),e&&this.emit("reconnection",t),this.reconnectionAttempts=0}else if(this.isPong(e))this.ping();else try{this.message(e)}catch(t){console.warn(t)}})}message(t){t=JSON.parse(t);const[e,s,i]=t;if("string"==typeof e)switch(e){case"rpc":this.rpc(t);break;default:this.emit(e,s)}}rpc(t){const[e,s,i]=t;"[object Object]"===Object.prototype.toString.call(s)&&s.hasOwnProperty("status")&&Number.isInteger(s.status)&&s.status>=400?this.errorCallbacks[i]&&this.errorCallbacks[i](s):this.responseCallbacks[i]&&this.responseCallbacks[i](s),this.responseCallbacks[i]&&delete this.responseCallbacks[i],this.errorCallbacks[i]&&delete this.errorCallbacks[i],clearTimeout(this.rpcTimeouts[i])}serializeMessage(t,e,s){let i=[t,e,s];return i=JSON.stringify(i)}cast(t,e=null,s=null,i=null,n={}){let o;n=Object.assign({timeout:this.opt.response_timeout},n),s&&(o=++this.rpcId,this.responseCallbacks[o]=s,this.errorCallbacks[o]=i,this.rpcTimeouts[o]=setTimeout(()=>{delete this.responseCallbacks[o],this.errorCallbacks[o]&&(this.errorCallbacks[o](null),delete this.errorCallbacks[o])},n.timeout));const r=this.serializeMessage(t,e,o);this.socket.send(r)}isReadyFlag(t){return 11===Number(t)}isPong(t){return 10===Number(t)}ping(){this.socket.send(this.isReactNative?"9":9)}} |
{ | ||
"name": "bridge.io-client", | ||
"version": "2.0.7", | ||
"version": "2.0.8", | ||
"description": "The client-side implementation of Bridge.IO", | ||
"main": "dist/bridge.io.js", | ||
"types": "index.d.ts", | ||
"scripts": { | ||
@@ -7,0 +8,0 @@ "test": "echo \"Error: no test specified\" && exit 1" |
@@ -7,3 +7,3 @@ # Bridge.IO Client 2.x | ||
```html | ||
<script src="https://cdn.jsdelivr.net/npm/bridge.io-client@2.0.7/dist/bridge.io.min.js"></script> | ||
<script src="https://cdn.jsdelivr.net/npm/bridge.io-client@2.0.8/dist/bridge.io.min.js"></script> | ||
``` | ||
@@ -10,0 +10,0 @@ |
11098
7.33%6
20%177
12.03%