UNPKG

web-events-client

Version:

Event-oriented interface for client and server communication based on WebSocket protocol. It's a client part of this interface.

1 lines 939 B
function webEvents(e,n){n||(n=e,e=null);function t(e,n){var t,c=e.apply(i,n);"object"==typeof c&&(c instanceof Array?(t=c[0],n=c.slice(1),o.apply(i,[t].concat(n))):(t=c.type,delete(n=c).type,i.emit(t,n)))}function o(e){var n=arguments;if(c.readyState==WebSocket.CONNECTING)return setTimeout(function(){o.apply(i,n)},4);n=Array.prototype.slice.call(arguments,1),c.send(JSON.stringify({type:e,args:n}))}var c=null,i=function(){return o.apply(null,arguments),i};function r(){(c=new WebSocket(null==e?"ws://"+location.hostname:e)).onopen=function(){n.connection&&t(n.connection)},c.onmessage=function(e){var o;try{o=JSON.parse(e.data)}catch(e){return}"string"==typeof o.type&&n[o.type]&&t(n[o.type],o.args)},c.onclose=function(){n.close&&t(n.close)}}return i.emit=o,i.disconnect=function(){c.close()},i.reconnect=function(){c.readyState==WebSocket.OPEN&&c.close(),r()},i.version="2.2.3",r(),i}void 0===window.events&&(window.events=webEvents);