filesbox/web/dist/design/designstatic/chatroom/js/reconnecting-websocket.min.js

2 lines
3.0 KiB
JavaScript
Raw Normal View History

2024-07-26 06:10:54 +00:00
(function(t,u){typeof define=="function"&&define.amd?define([],u):typeof module!="undefined"&&module.exports?module.exports=u():t.ReconnectingWebSocket=u()})(this,function(){function t(u,m,d){function r(n,a){var p=document.createEvent("CustomEvent");return p.initCustomEvent(n,!1,!1,a),p}var b={debug:!1,automaticOpen:!0,reconnectInterval:1e3,maxReconnectInterval:3e4,reconnectDecay:1.5,timeoutInterval:2e3};d||(d={});for(var l in b)this[l]=typeof d[l]!="undefined"?d[l]:b[l];this.url=u,this.reconnectAttempts=0,this.readyState=WebSocket.CONNECTING,this.protocol=null;var c,e=this,f=!1,g=!1,o=document.createElement("div");o.addEventListener("open",function(n){e.onopen(n)}),o.addEventListener("close",function(n){e.onclose(n)}),o.addEventListener("connecting",function(n){e.onconnecting(n)}),o.addEventListener("message",function(n){e.onmessage(n)}),o.addEventListener("error",function(n){e.onerror(n)}),this.addEventListener=o.addEventListener.bind(o),this.removeEventListener=o.removeEventListener.bind(o),this.dispatchEvent=o.dispatchEvent.bind(o),this.open=function(n){c=new WebSocket(e.url,m||[]),n||o.dispatchEvent(r("connecting")),(e.debug||t.debugAll)&&console.debug("ReconnectingWebSocket","attempt-connect",e.url);var a=c,p=setTimeout(function(){(e.debug||t.debugAll)&&console.debug("ReconnectingWebSocket","connection-timeout",e.url),g=!0,a.close(),g=!1},e.timeoutInterval);c.onopen=function(){clearTimeout(p),(e.debug||t.debugAll)&&console.debug("ReconnectingWebSocket","onopen",e.url),e.protocol=c.protocol,e.readyState=WebSocket.OPEN,e.reconnectAttempts=0;var i=r("open");i.isReconnect=n,n=!1,o.dispatchEvent(i)},c.onclose=function(i){if(clearTimeout(v),c=null,f)e.readyState=WebSocket.CLOSED,o.dispatchEvent(r("close"));else{e.readyState=WebSocket.CONNECTING;var s=r("connecting");s.code=i.code,s.reason=i.reason,s.wasClean=i.wasClean,o.dispatchEvent(s),n||g||((e.debug||t.debugAll)&&console.debug("ReconnectingWebSocket","onclose",e.url),o.dispatchEvent(r("close")));var v=e.reconnectInterval*Math.pow(e.reconnectDecay,e.reconnectAttempts);setTimeout(function(){e.reconnectAttempts++,e.open(!0)},v>e.maxReconnectInterval?e.maxReconnectInterval:v)}},c.onmessage=function(i){(e.debug||t.debugAll)&&console.debug("ReconnectingWebSocket","onmessage",e.url,i.data);var s=r("message");s.data=i.data,o.dispatchEvent(s)},c.onerror=function(i){(e.debug||t.debugAll)&&console.debug("ReconnectingWebSocket","onerror",e.url,i),o.dispatchEvent(r("error"))}},this.automaticOpen==1&&this.open(!1),this.send=function(n){if(c)return(e.debug||t.debugAll)&&console.debug("ReconnectingWebSocket","send",e.url,n),c.send(n);throw"INVALID_STATE_ERR : Pausing to reconnect websocket"},this.close=function(n,a){typeof n=="undefined"&&(n=1e3),f=!0,c&&c.close(n,a)},this.refresh=function(){c&&c.close()}}return t.prototype.onopen=function(){},t.prototype.onclose=function(){},t.prototype.onconnecting=function(){},t.prototype.onmessage=function(){},t.prototype.onerror=function(){},t.debugAll=!1,t.CONNECTING=WebSocket.CONNECTING,t.OPEN=WebSocket.OPEN,t.CLOSING=WebSocket.CLOSING,t.CLOSED=WebSocket.CLOSED,t});