Skip to content

Instantly share code, notes, and snippets.

@iwconfig
Created March 15, 2017 02:08
Show Gist options
  • Save iwconfig/9cee62d942f5cbd1cc0b5acd8606220b to your computer and use it in GitHub Desktop.
Save iwconfig/9cee62d942f5cbd1cc0b5acd8606220b to your computer and use it in GitHub Desktop.
Minified version of reconnecting-websocket.js. See https://github.com/bebo/reconnecting-websocket (c) Joe Walnes
!function(a,b){"function"==typeof define&&define.amd?define([],b):"undefined"!=typeof module&&module.exports?module.exports=b():a.ReconnectingWebSocket=b()}(this,function(){function a(b,c,d){function l(a,b){var c=document.createEvent("CustomEvent");return c.initCustomEvent(a,!1,!1,b),c}var e={debug:!1,automaticOpen:!0,reconnectInterval:1e3,maxReconnectInterval:3e4,reconnectDecay:1.5,timeoutInterval:2e3,maxReconnectAttempts:null,binaryType:"blob"};d||(d={});for(var f in e)"undefined"!=typeof d[f]?this[f]=d[f]:this[f]=e[f];this.url=b,this.reconnectAttempts=0,this.readyState=WebSocket.CONNECTING,this.protocol=null;var h,g=this,i=!1,j=!1,k=document.createElement("div");k.addEventListener("open",function(a){g.onopen(a)}),k.addEventListener("close",function(a){g.onclose(a)}),k.addEventListener("closing",function(a){g.onclosing(a)}),k.addEventListener("connecting",function(a){g.onconnecting(a)}),k.addEventListener("onmaxretry",function(a){g.onmaxretry(a)}),k.addEventListener("message",function(a){g.onmessage(a)}),k.addEventListener("error",function(a){g.onerror(a)}),this.addEventListener=k.addEventListener.bind(k),this.removeEventListener=k.removeEventListener.bind(k),this.dispatchEvent=k.dispatchEvent.bind(k),this.open=function(b){if(h=new WebSocket(g.url,c||[]),h.binaryType=this.binaryType,b){if(this.maxReconnectAttempts&&this.reconnectAttempts>this.maxReconnectAttempts)return void k.dispatchEvent(l("onmaxretry"))}else k.dispatchEvent(l("connecting")),this.reconnectAttempts=0;(g.debug||a.debugAll)&&console.debug("ReconnectingWebSocket","attempt-connect",g.url);var d=h,e=setTimeout(function(){(g.debug||a.debugAll)&&console.debug("ReconnectingWebSocket","connection-timeout",g.url),j=!0,d.close(),j=!1},g.timeoutInterval);h.onopen=function(c){clearTimeout(e),(g.debug||a.debugAll)&&console.debug("ReconnectingWebSocket","onopen",g.url),g.protocol=h.protocol,g.readyState=WebSocket.OPEN,g.reconnectAttempts=0;var d=l("open");d.isReconnect=b,b=!1,k.dispatchEvent(d)},h.onclose=function(c){if(clearTimeout(e),h=null,i)g.readyState=WebSocket.CLOSED,k.dispatchEvent(l("close"));else{g.readyState=WebSocket.CONNECTING;var d=g.reconnectInterval*Math.pow(g.reconnectDecay,g.reconnectAttempts),f=d>g.maxReconnectInterval?g.maxReconnectInterval:d,m=l("connecting");m.code=c.code,m.reason=c.reason,m.wasClean=c.wasClean,m.reconnectAttempts=g.reconnectAttempts,m.retryTimeout=f,k.dispatchEvent(m),b||j||((g.debug||a.debugAll)&&console.debug("ReconnectingWebSocket","onclose",g.url),k.dispatchEvent(l("close"))),setTimeout(function(){g.reconnectAttempts++,g.open(!0)},f)}},h.onmessage=function(b){(g.debug||a.debugAll)&&console.debug("ReconnectingWebSocket","onmessage",g.url,b.data);var c=l("message");c.data=b.data,k.dispatchEvent(c)},h.onerror=function(b){(g.debug||a.debugAll)&&console.debug("ReconnectingWebSocket","onerror",g.url,b),k.dispatchEvent(l("error"))}},1==this.automaticOpen&&this.open(!1),this.send=function(b){if(h)return(g.debug||a.debugAll)&&console.debug("ReconnectingWebSocket","send",g.url,b),h.send(b);throw"INVALID_STATE_ERR : Pausing to reconnect websocket"},this.close=function(a,b){"undefined"==typeof a&&(a=1e3),i=!0,h&&(g.readyState=WebSocket.CLOSING,k.dispatchEvent(l("closing")),h.close(a,b))},this.refresh=function(){h&&(g.readyState=WebSocket.CLOSING,k.dispatchEvent(l("closing")),(g.debug||a.debugAll)&&console.debug("ReconnectingWebSocket","refresh",g.url,g.readyState),h.close())}}if("WebSocket"in window)return a.prototype.onopen=function(a){},a.prototype.onclose=function(a){},a.prototype.onclosing=function(a){},a.prototype.onconnecting=function(a){},a.prototype.onmaxretry=function(a){},a.prototype.onmessage=function(a){},a.prototype.onerror=function(a){},a.debugAll=!1,a.CONNECTING=WebSocket.CONNECTING,a.OPEN=WebSocket.OPEN,a.CLOSING=WebSocket.CLOSING,a.CLOSED=WebSocket.CLOSED,a});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment