var RelayRConnection = {}; RelayRConnection = (function() { var readyCalled = false; var web, transport; var routeWithoutScheme = 'linccms-api.ra.de/api/v2/~relayr'; var route = 'https://linccms-api.ra.de/api/v2/~relayr'; transport = { websocket: { waitForConnection: function (callback, interval) { var s = this; if (s.socket.readyState === 1) { callback(); } else { console.log('%c-> websocket: connection is not ready, waiting ' + interval + 'ms', 'color:yellow'); setTimeout(function () { s.waitForConnection(callback, interval); }, interval); } }, connect: function(c) { var s = this; s.socket = new WebSocket("wss://" + routeWithoutScheme + "/ws?connectionId=" + transport.ConnectionId); s.socket.onclose = function(evt) { console.log('%c-> websocket: connection closed', 'color:orange', transport.ConnectionId); setTimeout(function() { web.n(); // renegotiate }, 2000); }; s.socket.onmessage = function(evt) { c(evt.data); }; s.socket.onerror = function(evt) { console.log('%c-> websocket: connection error', 'color:red', evt); setTimeout(function() { s.connect(c); }); }; s.socket.onopen = function(evt) { readyCalled ? console.log('%c-> websocket: connection opened %c(readyCalled == true)', 'color:green', 'color:red', evt) : console.log('%c-> websocket: connection opened', 'color:green', evt); if (!readyCalled) { RelayRConnection.r(); readyCalled = true; } }; }, send: function (data) { var s = this; s.waitForConnection(function () { s.socket.send(data); }, 500); } }, longpoll: { connect: function(c) { if (!readyCalled) { RelayRConnection.r(); readyCalled = true; } var retry; retry = function() { web.gj(route + '/longpoll?connectionId=' + transport.ConnectionId + '&_=' + new Date().getTime(), function(data) { if (data.responseText) { var reconn = JSON.parse(data.responseText); if (reconn.Z) { web.n(); } else { c(data); retry(); } } else { web.n(); } }); }; retry(); }, send: function(data) { var s = this; web.p(route + '/call?connectionId=' + transport.ConnectionId + '&_=' + new Date().getTime(), data, null, "json", null); } } }; web = (function() { return { x: function() { var xd; if (window.XMLHttpRequest) { xd = new XMLHttpRequest(); } else { xd = new ActiveXObject("Microsoft.XMLHTTP"); } return xd; }, g: function(u, c) { var s = this; var xd = s.x(); xd.open('GET', u, true); xd.onreadystatechange = function() { if (xd.readyState === 4 && xd.status === 200) { c(xd); } }; xd.send(); }, gj: function(u, c) { var s = this; var xd = s.x(); xd.open('GET', u, true); xd.setRequestHeader("Content-type", "application/json"); xd.onreadystatechange = function() { if (xd.readyState === 4) { if (xd.status === 200) { c(xd); } } }; xd.send(); }, p: function(u, d, c, t, e) { var s = this; var xd = s.x(); xd.open('POST', u, true); xd.setRequestHeader("Content-type", "application/" + t); xd.onreadystatechange = function() { if (xd.readyState === 4) { if (xd.status === 200) { if (c) { c(xd); } } } }; xd.onerror = function() { if (e) { e(xd); } }; xd.send(d); window.onbeforeunload = function() { delete xd; xd = null; }; }, t: function() { if (!!window.WebSocket) { return "websocket"; } else { return "longpoll"; } // TODO: Implement SSE Circuit /*if (typeof EventSource !== 'undefined') { return "SSE"; }*/ }, n: function() { var s = this; var t = s.t(); web.p(route + "/negotiate?_=" + new Date().getTime(), JSON.stringify({ t: t }), function(result) { var obj = JSON.parse(result.responseText); transport.ConnectionId = obj.ConnectionID; setTimeout(function() { transport[t].connect(function(data) { var cobj; if (data.responseText && data.status && data.responseXML) { cobj = JSON.parse(data.responseText); } else { if (data.responseText == "") return; cobj = JSON.parse(data); } var lobj = RelayR[cobj.R].client; var args = []; for (var i = 0; i < cobj.A.length; i++) { args.push(cobj.A[i]); } lobj[cobj.M] && lobj[cobj.M].apply(lobj||window, args); }); }, 0); }, "json", function(result) { console.log('%c-> ~relayr: negotiate error', 'color:red', result); // error .. try again setTimeout(function() { s.n(); }, 2000); }); } }; })(); return { ready: function(r) { RelayRConnection.r = r; web.n(); }, callServer: function(r, f, a) { transport[web.t()].send(JSON.stringify({ S: true, C: transport.ConnectionId, R: r, M: f, A: a})); } }; })(); var RelayR = (function() { return { AccountRelay: { client: {}, server: { getGroupName: function() { RelayRConnection.callServer('AccountRelay', 'GetGroupName', Array.prototype.slice.call(arguments)); }, pushAccountUpdated: function() { RelayRConnection.callServer('AccountRelay', 'PushAccountUpdated', Array.prototype.slice.call(arguments)); }, pushNewDialog: function() { RelayRConnection.callServer('AccountRelay', 'PushNewDialog', Array.prototype.slice.call(arguments)); }, pushNewThreadMessage: function() { RelayRConnection.callServer('AccountRelay', 'PushNewThreadMessage', Array.prototype.slice.call(arguments)); }, pushPermissionsUpdated: function() { RelayRConnection.callServer('AccountRelay', 'PushPermissionsUpdated', Array.prototype.slice.call(arguments)); }, pushThreadStateChanged: function() { RelayRConnection.callServer('AccountRelay', 'PushThreadStateChanged', Array.prototype.slice.call(arguments)); }, subscribe: function() { RelayRConnection.callServer('AccountRelay', 'Subscribe', Array.prototype.slice.call(arguments)); }, unsubscribe: function() { RelayRConnection.callServer('AccountRelay', 'Unsubscribe', Array.prototype.slice.call(arguments)); }, }, }, CompanyRelay: { client: {}, server: { getGroupName: function() { RelayRConnection.callServer('CompanyRelay', 'GetGroupName', Array.prototype.slice.call(arguments)); }, pushCompanyProfileChanged: function() { RelayRConnection.callServer('CompanyRelay', 'PushCompanyProfileChanged', Array.prototype.slice.call(arguments)); }, pushCompanySubscriptionChanged: function() { RelayRConnection.callServer('CompanyRelay', 'PushCompanySubscriptionChanged', Array.prototype.slice.call(arguments)); }, pushNewThreadMessage: function() { RelayRConnection.callServer('CompanyRelay', 'PushNewThreadMessage', Array.prototype.slice.call(arguments)); }, pushThreadStateChanged: function() { RelayRConnection.callServer('CompanyRelay', 'PushThreadStateChanged', Array.prototype.slice.call(arguments)); }, subscribe: function() { RelayRConnection.callServer('CompanyRelay', 'Subscribe', Array.prototype.slice.call(arguments)); }, unsubscribe: function() { RelayRConnection.callServer('CompanyRelay', 'Unsubscribe', Array.prototype.slice.call(arguments)); }, }, }, }; })();