vue-simple-websocket
Version:
A simple native reconnecting websocket for Vue.js
3 lines (2 loc) • 1.73 kB
JavaScript
function n(n,e){for(var t=0;t<e.length;t++){var o=e[t];o.enumerable=o.enumerable||!1,o.configurable=!0,"value"in o&&(o.writable=!0),Object.defineProperty(n,o.key,o)}}var e=function(){function e(n,t){(function(n,e){if(!(n instanceof e))throw new TypeError("Cannot call a class as a function")})(this,e),console.log(n,t),this.instance=null,this.url=n,this.options=t||this.defaultOptions(),this.options&&(this.reconnectEnabled=t.reconnectEnabled||!1,this.reconnectEnabled&&(this.reconnectInterval=t.reconnectInterval)),this.onOpen=null,this.onMessage=null,this.onClose=null,this.onError=null}return t=e,(o=[{key:"defaultOptions",value:function(){return{reconnectEnabled:!1,reconnectInterval:0}}},{key:"connect",value:function(){var n=this;this.instance=new WebSocket(this.url),this.instance.onopen=function(){"function"==typeof n.onOpen&&n.onOpen()},this.instance.onmessage=function(e){"function"==typeof n.onMessage&&n.onMessage(e)},this.instance.onclose=function(e){"function"==typeof n.onClose&&n.onClose(e),!e.wasClean&&n.reconnectEnabled&&n.reconnect()},this.instance.onerror=function(e){"function"==typeof n.onError&&n.onError(e)}}},{key:"reconnect",value:function(){var n=this;delete this.instance,setTimeout((function(){n.connect()}),this.reconnectInterval)}},{key:"send",value:function(n){this.instance.send(n)}},{key:"sendObj",value:function(n){this.instance.send(JSON.stringify(n))}},{key:"removeListeners",value:function(){this.onOpen=null,this.onMessage=null,this.onClose=null,this.onError=null}}])&&n(t.prototype,o),i&&n(t,i),e;var t,o,i}(),t={install:function(n,t,o){var i=new e(t,o);i.connect(),n.config.globalProperties.$socketClient=i}};module.exports=t;
//# sourceMappingURL=VueSimpleWebSocket.js.map
;