forked from desertbit/glue
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathglue.js
1 lines (1 loc) · 6.92 KB
/
glue.js
1
var glue=function(e,n){"use strict";var t,o,i,c,r,a,s,u,l=function(){var t,o={};return o.open=function(){try{var i;i=e.match("^https://")?"wss"+e.substr(5):"ws"+e.substr(4),i+=n.baseURL+"ws",t=new WebSocket(i),t.onmessage=function(e){o.onMessage(e.data.toString())},t.onerror=function(e){var n="the websocket closed the connection with ";n+=e.code?"the error code: "+e.code:"an error.",o.onError(n)},t.onclose=function(){o.onClose()},t.onopen=function(){o.onOpen()}}catch(c){o.onError()}},o.send=function(e){t.send(e)},o.reset=function(){t&&t.close(),t=void 0},o},d=function(){var t,o,i,c=e+n.baseURL+"ajax",r=8e3,a=45e3,s={Timeout:"t",Closed:"c"},u={Delimiter:"&",Init:"i",Push:"u",Poll:"o"},l={},d=!1,f=!1,g=function(){i=function(){},d&&d.abort(),f&&f.abort()},v=function(){g(),l.onClose()},m=function(e){g(),e=e?"the ajax socket closed the connection with the error: "+e:"the ajax socket closed the connection with an error.",l.onError(e)},h=function(e,n){f=$.ajax({url:c,success:function(e){f=!1,n&&n(e)},error:function(e,n){f=!1,m(n)},type:"POST",data:e,dataType:"text",timeout:r})};return i=function(){d=$.ajax({url:c,success:function(e){if(d=!1,e==s.Timeout)return void i();if(e==s.Closed)return void v();var n=e.indexOf(u.Delimiter);return 0>n?void m("ajax socket: failed to split poll token from data!"):(o=e.substring(0,n),e=e.substr(n+1),i(),void l.onMessage(e))},error:function(e,n){d=!1,m(n)},type:"POST",data:u.Poll+t+u.Delimiter+o,dataType:"text",timeout:a})},l.open=function(){h(u.Init,function(e){var n=e.indexOf(u.Delimiter);return 0>n?void m("ajax socket: failed to split uid and poll token from data!"):(t=e.substring(0,n),o=e.substr(n+1),i(),void l.onOpen())})},l.send=function(e){h(u.Push+t+u.Delimiter+e)},l.reset=function(){g()},l},f="1.8.2",g="m",v={WebSocket:"WebSocket",AjaxSocket:"AjaxSocket"},m={Len:2,Init:"in",Ping:"pi",Pong:"po",Close:"cl",Invalid:"iv",DontAutoReconnect:"dr",ChannelData:"cd"},h={Disconnected:"disconnected",Connecting:"connecting",Reconnecting:"reconnecting",Connected:"connected"},p={baseURL:"/glue/",forceSocketType:!1,connectTimeout:1e4,pingInterval:35e3,pingReconnectTimeout:5e3,reconnect:!0,reconnectDelay:1e3,reconnectDelayMax:5e3,reconnectAttempts:10,resetSendBufferTimeout:1e4},b=!1,k=!1,D=h.Disconnected,T=0,x=!1,y=!1,S=!1,C=!1,M=[],w=!1,R=!1,O=!1,I=[],L="",P=function(){var e="&",n={};return n.unmarshalValues=function(n){if(!n)return!1;var t=n.indexOf(e),o=parseInt(n.substring(0,t),10);if(n=n.substring(t+1),0>o||o>n.length)return!1;var i=n.substr(0,o),c=n.substr(o);return{first:i,second:c}},n.marshalValues=function(n,t){return String(n.length)+e+n+t},n}(),j=function(){var e={},n={},t=function(e){var n={onMessageFunc:function(){}};return n.instance={onMessage:function(e){n.onMessageFunc=e},send:function(n,t){return n?a(m.ChannelData,P.marshalValues(e,n),t):-1}},n};return e.get=function(e){if(!e)return!1;var o=n[e];return o?o.instance:(o=t(e),n[e]=o,o.instance)},e.emitOnMessage=function(e,t){if(e&&t){var o=n[e];if(!o)return void console.log("glue: channel '"+e+"': emit onMessage event: channel does not exists");try{o.onMessageFunc(t)}catch(i){return void console.log("glue: channel '"+e+"': onMessage event call failed: "+i.message)}}},e}();r=function(e){return b?O?void b.send(e):void I.push(e):void 0};var A=function(){if(0!==I.length){for(var e=0;e<I.length;e++)r(I[e]);I=[]}},U=function(){R=!1,w!==!1&&(clearTimeout(w),w=!1)},W=function(){w!==!1||R||(w=setTimeout(function(){if(w=!1,R=!0,0!==M.length){for(var e,n=0;n<M.length;n++)if(e=M[n],e.discardCallback&&$.isFunction(e.discardCallback))try{e.discardCallback(e.data)}catch(t){console.log("glue: failed to call discard callback: "+t.message)}u("discard_send_buffer"),M=[]}},n.resetSendBufferTimeout))},E=function(){if(U(),0!==M.length){for(var e,n=0;n<M.length;n++)e=M[n],r(e.cmd+e.data);M=[]}};a=function(e,n,t){return n||(n=""),b&&D===h.Connected?(r(e+n),1):R?(t&&$.isFunction(t)&&t(n),-1):(W(),M.push({cmd:e,data:n,discardCallback:t}),0)};var F=function(){y!==!1&&(clearTimeout(y),y=!1)},q=function(){F(),y=setTimeout(function(){y=!1,u("connect_timeout"),s()},n.connectTimeout)},V=function(){S!==!1&&(clearTimeout(S),S=!1),C!==!1&&(clearTimeout(C),C=!1)},_=function(){V(),S=setTimeout(function(){S=!1,r(m.Ping),C=setTimeout(function(){C=!1,u("timeout"),s()},n.pingReconnectTimeout)},n.pingInterval)},z=function(){return k?void(b=o()):T>1?(o=d,b=o(),void(i=v.AjaxSocket)):(!n.forceSocketType&&window.WebSocket||n.forceSocketType===v.WebSocket?(o=l,i=v.WebSocket):(o=d,i=v.AjaxSocket),void(b=o()))},B=function(e){return e=JSON.parse(e),e.socketID?(L=e.socketID,O=!0,A(),D=h.Connected,u("connected"),void setTimeout(E,0)):(c(),void console.log("glue: socket initialization failed: invalid initialization data received"))},J=function(){z(),b.onOpen=function(){F(),T=0,k=!0,_();var e={version:f};e=JSON.stringify(e),b.send(m.Init+e)},b.onClose=function(){s()},b.onError=function(e){u("error",[e]),s()},b.onMessage=function(e){if(_(),e.length<m.Len)return void console.log("glue: received invalid data from server: data is too short.");var n=e.substr(0,m.Len);if(e=e.substr(m.Len),n===m.Ping)r(m.Pong);else if(n===m.Pong);else if(n===m.Invalid)console.log("glue: server replied with an invalid request notification!");else if(n===m.DontAutoReconnect)x=!0,console.log("glue: server replied with an don't automatically reconnect request. This might be due to an incompatible protocol version.");else if(n===m.Init)B(e);else if(n===m.ChannelData){var t=P.unmarshalValues(e);if(!t)return void console.log("glue: server requested an invalid channel data request: "+e);j.emitOnMessage(t.first,t.second)}else console.log("glue: received invalid data from server with command '"+n+"' and data '"+e+"'!")},setTimeout(function(){T>0?(D=h.Reconnecting,u("reconnecting")):(D=h.Connecting,u("connecting")),q(),b.open()},0)},N=function(){F(),V(),O=!1,L="",I=[],b&&(b.onOpen=b.onClose=b.onMessage=b.onError=function(){},b.reset(),b=!1)};if(s=function(){if(N(),n.reconnectAttempts>0&&T>n.reconnectAttempts||n.reconnect===!1||x)return D=h.Disconnected,void u("disconnected");T+=1;var e=n.reconnectDelay*T;e>n.reconnectDelayMax&&(e=n.reconnectDelayMax),setTimeout(function(){J()},e)},c=function(){b&&(r(m.Close),N(),D=h.Disconnected,u("disconnected"))},t=j.get(g),e||(e=window.location.protocol+"//"+window.location.host),!e.match("^http://")&&!e.match("^https://"))return void console.log("glue: invalid host: missing 'http://' or 'https://'!");n=$.extend(p,n),n.reconnectDelayMax<n.reconnectDelay&&(n.reconnectDelayMax=n.reconnectDelay),0!==n.baseURL.indexOf("/")&&(n.baseURL="/"+n.baseURL),"/"!==n.baseURL.slice(-1)&&(n.baseURL=n.baseURL+"/"),J();var H={version:function(){return f},type:function(){return i},state:function(){return D},socketID:function(){return L},send:function(e,n){t.send(e,n)},onMessage:function(e){t.onMessage(e)},on:function(){var e=$(H);e.on.apply(e,arguments)},reconnect:function(){D===h.Disconnected&&(T=0,x=!1,s())},close:function(){c()},channel:function(e){return j.get(e)}};return u=function(){var e=$(H);e.triggerHandler.apply(e,arguments)},H};