// ------------------- _masterSocket functions ----------------------- var _masterSocket; var _masterSocketHeartbeatsLost; var _masterWatchingHeartbeat = false; var _masterSocketLastHeartbeat = -1; var _masterAuthenticationTicket = ""; function _masterInitializeSocket(authenticationTicket) { // Our first call should always be with an auth ticket; save this for later use if (authenticationTicket === undefined) { authenticationTicket = _masterAuthenticationTicket; } else { _masterAuthenticationTicket = authenticationTicket; } // If we're being called to restore communications in case of a heartbeat failure, // close the channel first or the server will think we're still here if (_masterSocket != null) { _masterSocket.close(); _masterSocket = null; } var socketUrl = getMasterSocketAddress() + "?Auth=" + authenticationTicket; // Create instance and open socket _masterSocket = new WebSocket(socketUrl); // Set events _masterSocket.onopen = function(data) { console.log("MasterSocket.OnOpen();"); if (_error_ClientSocketLost) { _error_ClientSocketLost = false; updateMalfunctions(); } // If this is the first .open, start the heartbeat watcher if (!_masterWatchingHeartbeat) { watchHeartbeat(); } }; _masterSocket.onclose = function (data) { console.log("MasterSocket.OnClose();"); _error_ClientSocketLost = true; updateMalfunctions(); if (!_masterWatchingHeartbeat) { watchHeartbeat(); // begin watch heartbeat if open never happened } }; _masterSocket.onerror = function (data) { console.log("MasterSocket.OnError();"); if (!_error_ClientSocketLost) { alertify.error("WARNING: Socket connection error - realtime updates will not be available"); _error_ClientSocketLost = true; updateMalfunctions(); } }; _masterSocket.onmessage = function (data) { console.log(data.data); var message = $.parseJSON(data.data); if (message.MessageType == "Heartbeat") { _masterSocketLastHeartbeat = new Date().getTime(); console.log("Heartbeat"); if (_masterSocketHeartbeatsLost) { console.log(" - receiving heartbeats again"); _masterSocketHeartbeatsLost = false; updateMalfunctions(); } } else if (message.MessageType == "BitcoinReceived") { // console.log("Currency is " + message.Currency); // console.log(message); var handled = false; if (typeof pageBitcoinReceived === "function") { // if it's defined on the page indicating the page handles it handled = pageBitcoinReceived(message.Address, message.Hash, message.Satoshis, message.Cents, message.Currency); } if (!handled) { // Otherwise make a log note at the bottom right of the screen alertify.log("Bitcoin received: " + message.Currency + " " + message.CentsFormatted); } } else if (message.MessageType == "Malfunctions") { updateMalfunctions (message.MalfunctionsList); } else if (message.MessageType == "SandboxUpdate") { if (odoLocalParticipation != undefined) { // Real ugly accessing specific page elements here, but it's temporary odoLocalParticipation.innerHTML = message.Local; odoGlobalParticipation.innerHTML = 12345678 + message.Local * 5; odoActiveParticipation.innerHTML = 123412 + message.Local * 5; odoProfitLossToDate.innerHTML = (message.Profit / 100.0) + 0.001; } } }; } function watchHeartbeat() { console.log("WatchHeartbeat()"); _masterWatchingHeartbeat = true; // Check again for a new heartbeat in ten seconds setTimeout(function () { watchHeartbeat(); }, 10000); // Have we lost the socket connection? If so, use this to try reconnecting if (_error_ClientSocketLost) { console.log(" - trying to reinit socket"); _masterInitializeSocket(); } // If we got a heartbeat more than fifteen seconds ago, we've lost the heartbeat console.log(" - checking timestamps"); if (_masterSocketLastHeartbeat != -1 && !_masterSocketHeartbeatsLost) { // if we've received any at all var curTime = new Date().getTime(); var diff = (curTime - _masterSocketLastHeartbeat) / 1000; console.log(" - last heartbeat was " + diff + " seconds ago"); if (diff > 15) { console.log(" - Heartbeats LOST"); _masterSocketHeartbeatsLost = true; updateMalfunctions(); } } else { console.log(" - no previous heartbeat received"); } } function getMasterSocketAddress() { if (location.host.indexOf ("localhost") >= 0) { // Assume dev environment, go for sandbox socket return 'ws://sandbox.swarmops.com/ws/Front'; } else { var protocol = ('https:' == document.location.protocol ? 'wss://' : 'ws://'); return protocol + location.host + "/ws/Front"; } } function updateMalfunctions(serverList) { console.log("UpdateMalfunctions();"); if (serverList === undefined) { serverList = JSON.parse(JSON.stringify(_master_LastServerMalfunctionsList)); // deep copy } else { _master_LastServerMalfunctionsList = JSON.parse(JSON.stringify(serverList)); // deep copy } if (serverList === undefined) { // init edge case serverList = []; _master_LastServerMalfunctionsList = []; } if (_error_ClientSocketLost) { console.log("Foo2"); serverList.push(constructMalfunctionData("ClientSocket", _errorDisplay_clientSocketLost)); } else if (_masterSocketHeartbeatsLost) { console.log("Foo1"); serverList.push(constructMalfunctionData("ClientHeartbeat", _errorDisplay_clientHeartbeatLost)); } console.log("Bar"); updateListBox($('#divMalfunctionsList'), serverList); } function constructMalfunctionData(id, text, icon, link) { var newItem = {}; newItem.Id = id; newItem.Text = text; newItem.Icon = icon; newItem.Link = link; return newItem; } var _master_LastServerMalfunctionsList = []; var _error_ClientSocketLost = false; // ------------------- List updating -------------------------- function updateListBox(box, listData) { // list is an array of object { id, text } // box is a div-div-ul-li nest containing the li:s with the id // the function syncs the box to the list with some nice fades console.log("UpdateListBox();"); // Is the box visible right now? var boxVisible = $(box).is(":visible"); // Step 1: Iterate through list, build id array console.log(" - step 1"); var listArray = listData; console.log(listArray); var idListLookup = {}; if (listData.length > 0) { listArray.forEach(function(item, index) { idListLookup[item.Id] = item; }); } // Step 2: Iterate through box, change texts of matching ids, // remove items that aren't in list, build id array console.log(" - step 2"); var idBoxLookup = {}; var listContainer = $(box).parent().parent(); var listElements = $(box).children(); if (listElements.length > 0) { listElements.children().each(function (index) { console.log($(this)); var elementId = $(this).attr("rel"); console.log(elementId); idBoxLookup[elementId] = true; var testLookup = idListLookup[elementId]; console.log(testLookup); if (testLookup === undefined) { // displayed item is NOT present in supplied list, and should be removed $(this).slideUp(400, function () { $(this).remove(); }); } else { // displayed item is present in supplied list // TODO: update text } }); } // Step 3: Iterate through list, add missing items console.log(" - step 3"); if (listElements.length == 0) { // If there's not even an UL element (yet), add one and move the pointer to it, // so we can add any missing items $(box).append ($("