added ping pong to keep ws connections alive between supernodes

This commit is contained in:
Abhishek Sinha 2020-06-01 19:07:13 +05:30
parent 8c732e23cd
commit c0cc0e177c

View File

@ -2,9 +2,12 @@
<html lang="en"> <html lang="en">
<head> <head>
<meta charset="UTF-8"> <meta charset="UTF-8">
<link rel="shortcut icon" href="#">
<meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Local Bitcoin++</title> <title>Local Bitcoin++</title>
<style type="text/css"> <!-- <link rel="stylesheet" href="/home/aks/Documents/p2p/localbitcoinplusplus/supernode/server/web/playground/main.css"> -->
<style>
@import url("https://fonts.googleapis.com/css?family=Barlow:400,500,600,700&display=swap"); @import url("https://fonts.googleapis.com/css?family=Barlow:400,500,600,700&display=swap");
@import url("https://fonts.googleapis.com/css?family=Roboto:400,500&display=swap"); @import url("https://fonts.googleapis.com/css?family=Roboto:400,500&display=swap");
@import url("https://fonts.googleapis.com/css2?family=Source+Code+Pro&display=swap"); @import url("https://fonts.googleapis.com/css2?family=Source+Code+Pro&display=swap");
@ -1569,8 +1572,6 @@
} }
} }
/*# sourceMappingURL=main.css.map */ /*# sourceMappingURL=main.css.map */
</style> </style>
</head> </head>
<body data-theme='light'> <body data-theme='light'>
@ -14420,9 +14421,7 @@
extra_backup_ws[closestSu[0].data.id] = new WebSocket(url); extra_backup_ws[closestSu[0].data.id] = new WebSocket(url);
extra_backup_ws[closestSu[0].data.id].onopen = function (evt) { extra_backup_ws[closestSu[0].data.id].onopen = function (evt) {
//if (extra_backup_ws[closestSu[0].data.id].bufferedAmount == 0) {
extra_backup_ws[closestSu[0].data.id].send(finalMessage); extra_backup_ws[closestSu[0].data.id].send(finalMessage);
//}
}; };
extra_backup_ws[closestSu[0].data.id].onclose = function (evt) { extra_backup_ws[closestSu[0].data.id].onclose = function (evt) {
console.info(`Closed extra conn ${evt.srcElement.url}`); console.info(`Closed extra conn ${evt.srcElement.url}`);
@ -15876,20 +15875,6 @@
// Also refresh deposited crypto balances // Also refresh deposited crypto balances
RM_WALLET.refresh_reserved_crypto_balances( RM_WALLET.refresh_reserved_crypto_balances(
localbitcoinplusplus.wallets.my_local_flo_address); localbitcoinplusplus.wallets.my_local_flo_address);
// Inform left side Supernodes you are back
const msg_obj = {};
msg_obj.protocol = '__ALL_SUPERNODES_MSG__';
msg_obj.event = 'supernode_came_back';
msg_obj.data = { subject_flo_id: userKeysObj.address };
msg_obj.initialSender = userKeysObj.address;
msg_obj.su_pubKey = userKeysObj.pubKeyHex;
msg_obj.hash = Crypto.SHA256(msg_obj);
msg_obj.sign = RM_WALLET.sign(
msg_obj.hash,
localbitcoinplusplus.wallets.MY_SUPERNODE_PRIVATE_KEY
);
reactor.dispatchEvent("informLeftSuperNode", msg_obj,
n = localbitcoinplusplus.master_configurations.MaxBackups);
} }
} catch (error) { } catch (error) {
throw new Error(error); throw new Error(error);
@ -16109,12 +16094,9 @@
tempWS[sn] = new WebSocket(url); tempWS[sn] = new WebSocket(url);
tempWS[sn].onopen = async function (evt) { tempWS[sn].onopen = async function (evt) {
//await localbitcoinplusplus.actions.delay(5000);
//if (tempWS[sn].bufferedAmount == 0) {
tempWS[sn].send(JSON.stringify(msg_obj)); tempWS[sn].send(JSON.stringify(msg_obj));
await localbitcoinplusplus.actions.delay(5000); await localbitcoinplusplus.actions.delay(5000);
tempWS[sn].close(); tempWS[sn].close();
//}
}; };
tempWS[sn].onclose = function (evt) { tempWS[sn].onclose = function (evt) {
console.info(`Closed ${evt.srcElement.url}`); console.info(`Closed ${evt.srcElement.url}`);
@ -16655,9 +16637,7 @@
extra_backup_ws[closestBackup] = new WebSocket(url); extra_backup_ws[closestBackup] = new WebSocket(url);
extra_backup_ws[closestBackup].onopen = function (evt) { extra_backup_ws[closestBackup].onopen = function (evt) {
//if (extra_backup_ws[closestBackup].bufferedAmount == 0) {
extra_backup_ws[closestBackup].send(finalMessage); extra_backup_ws[closestBackup].send(finalMessage);
//}
}; };
extra_backup_ws[closestBackup].onclose = function (evt) { extra_backup_ws[closestBackup].onclose = function (evt) {
console.info(`Closed extra conn ${evt.srcElement.url}`); console.info(`Closed extra conn ${evt.srcElement.url}`);
@ -16681,7 +16661,6 @@
} }
}); });
// SECTION: Misc functions // SECTION: Misc functions
function removeWhiteSpaces(text = '') { function removeWhiteSpaces(text = '') {
return text.replace(/\s/g, ''); return text.replace(/\s/g, '');
@ -16946,7 +16925,7 @@
notify('Please provide your FLO private key to Sign in.'); notify('Please provide your FLO private key to Sign in.');
showPopup('sign_in_popup', 'no'); showPopup('sign_in_popup', 'no');
async function app_login() { async function app_login(show_pk=true) {
try { try {
const RM_WALLET = new localbitcoinplusplus.wallets(); const RM_WALLET = new localbitcoinplusplus.wallets();
let pk_manual = document.getElementById('get_priv_key_field').value; let pk_manual = document.getElementById('get_priv_key_field').value;
@ -16960,6 +16939,7 @@
return reject(false); return reject(false);
} }
} else { } else {
if(show_pk===true) {
notify(`This is your LOGIN PRIVATE KEY: notify(`This is your LOGIN PRIVATE KEY:
<span class="breakable">${gen_new_keys.privateKeyWIF}</span> <span class="breakable">${gen_new_keys.privateKeyWIF}</span>
@ -16973,6 +16953,7 @@
Else you can lose your deposited assets like Bitcoins or Cash forever. Else you can lose your deposited assets like Bitcoins or Cash forever.
`, '', true, true, true); `, '', true, true, true);
} }
}
Object.defineProperty( Object.defineProperty(
localbitcoinplusplus.wallets, localbitcoinplusplus.wallets,
@ -17018,10 +16999,14 @@
} }
const signInBtn = document.getElementById('signInBtn'); const signInBtn = document.getElementById('signInBtn');
signInBtn.onclick=app_login; signInBtn.addEventListener('click', function() {
app_login(false);
}, false);
const newUserSignInBtn = document.getElementById('newUserSignInBtn'); const newUserSignInBtn = document.getElementById('newUserSignInBtn');
newUserSignInBtn.onclick=app_login; newUserSignInBtn.addEventListener('click', function() {
app_login(true);
}, false);
}); });
@ -24103,13 +24088,29 @@
) { ) {
this.ws_url = ws_url; this.ws_url = ws_url;
this.ws_connection = null; this.ws_connection = null;
this.ping_interval;
this.ping_timeout;
}); });
backupSupernodesWebSocketObject.prototype = { backupSupernodesWebSocketObject.prototype = {
connectWS() { connectWS() {
this.ws_connection = new WebSocket(this.ws_url); this.ws_connection = new WebSocket(this.ws_url);
const switchMyWS = new backupSupernodesWebSocketObject(); const switchMyWS = new backupSupernodesWebSocketObject();
ping= ()=> {
this.ws_connection.send('__ping__');
this.ping_timeout = setTimeout(function () {
console.log('----timout----', this.ws_connection);
this.ws_connection.close();
reactor.dispatchEvent("resolve_backup_ws_connections");
}.bind(this), 125000);
},
pong=()=> {
clearTimeout(this.ping_timeout);
console.log('recvd pong', this.ws_connection);
},
this.ws_connection.onopen = async function (evt) { this.ws_connection.onopen = async function (evt) {
this.ping_interval = setInterval(ping, 30000);
await reactor.dispatchEvent("backup_supernode_up", evt.srcElement.url); await reactor.dispatchEvent("backup_supernode_up", evt.srcElement.url);
await reactor.dispatchEvent("remove_extra_backup_connections"); await reactor.dispatchEvent("remove_extra_backup_connections");
@ -24120,6 +24121,7 @@
}.bind(this); }.bind(this);
this.ws_connection.onclose = function (evt) { this.ws_connection.onclose = function (evt) {
clearInterval(this.ping_interval);
reactor.dispatchEvent("backup_supernode_down", evt); reactor.dispatchEvent("backup_supernode_down", evt);
limit_function_calls(async function () { limit_function_calls(async function () {
await localbitcoinplusplus.actions.refresh_live_status_of_supernodes(true); await localbitcoinplusplus.actions.refresh_live_status_of_supernodes(true);
@ -24127,6 +24129,10 @@
}.bind(this); }.bind(this);
this.ws_connection.onmessage = function (evt) { this.ws_connection.onmessage = function (evt) {
let response = evt.data; let response = evt.data;
if (response == '__pong__') {
pong();
return;
}
console.log(`backup message: ${response}`); console.log(`backup message: ${response}`);
let is_corona_positive = quarantineCoronaElements(response); let is_corona_positive = quarantineCoronaElements(response);
if (is_corona_positive === true) throw new Error("Response failed sanitization test."); if (is_corona_positive === true) throw new Error("Response failed sanitization test.");
@ -24288,7 +24294,6 @@
return Promise.reject(ms); return Promise.reject(ms);
} }
//ideal_supernode = `ws://${nextClosestSupernodeElem[0].ip}:${nextClosestSupernodeElem[0].port}`;
ideal_supernode = `${WS}://${nextClosestSupernodeElem[0].ip}`; ideal_supernode = `${WS}://${nextClosestSupernodeElem[0].ip}`;
await startWebSocket(ideal_supernode); await startWebSocket(ideal_supernode);
@ -24334,6 +24339,7 @@
function startWebSocket(wsUri) { function startWebSocket(wsUri) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
websocket = new WebSocket(wsUri); websocket = new WebSocket(wsUri);
websocket.onopen = function (evt) { websocket.onopen = function (evt) {
resolve(onOpen(evt)); resolve(onOpen(evt));
}; };
@ -24361,6 +24367,9 @@
if (localbitcoinplusplus.master_configurations.supernodesPubKeys if (localbitcoinplusplus.master_configurations.supernodesPubKeys
.includes(localbitcoinplusplus.wallets.my_local_flo_public_key)) { .includes(localbitcoinplusplus.wallets.my_local_flo_public_key)) {
// Inform everyone you are back
await localbitcoinplusplus.actions.refresh_live_status_of_supernodes(true);
// delete buy and sell orders // delete buy and sell orders
let remove_promises = []; let remove_promises = [];
remove_promises.push(removeAllinDB('buyOrders')); remove_promises.push(removeAllinDB('buyOrders'));
@ -24391,21 +24400,27 @@
} }
function onClose(evt) { function onClose(evt) {
// Also close the backup ws conns console.warn(`[close] Connection closed, code=${evt.code} reason=${evt.reason}`);
if (localbitcoinplusplus.master_configurations.supernodesPubKeys if (localbitcoinplusplus.master_configurations.supernodesPubKeys
.includes(localbitcoinplusplus.wallets.my_local_flo_public_key)) { .includes(localbitcoinplusplus.wallets.my_local_flo_public_key)) {
// for (conn in localbitcoinplusplus.backupWS) { // Try to reconnect back
// localbitcoinplusplus.backupWS[conn].ws_connection.close();
// }
startWebSocket(evt.srcElement.url); startWebSocket(evt.srcElement.url);
} else { } else {
reactor.dispatchEvent("primary_supernode_down", evt); reactor.dispatchEvent("primary_supernode_down", evt);
} }
} }
function send_pong() {
websocket.send('__pong__');
}
// SECTION: PRIMARY CONNECTION MESSAGES // SECTION: PRIMARY CONNECTION MESSAGES
async function onMessage(evt) { async function onMessage(evt) {
var response = evt.data || evt; var response = evt.data || evt;
if (response === '__ping__') {
return send_pong();
}
console.log("RESPONSE: " + response); console.log("RESPONSE: " + response);
// Check if request is clean or not // Check if request is clean or not
@ -24536,6 +24551,7 @@
case "refresh_all_supernodes_status": case "refresh_all_supernodes_status":
reactor.dispatchEvent('resolve_backup_ws_connections'); reactor.dispatchEvent('resolve_backup_ws_connections');
localbitcoinplusplus.actions.refresh_live_status_of_supernodes(false);
break; break;
case "delete_this_user_data_from_backup": case "delete_this_user_data_from_backup":
@ -30181,6 +30197,8 @@
finalMessage = JSON.stringify(msgObj); finalMessage = JSON.stringify(msgObj);
} }
let setIntervalVal;
// The message is for a specific supernode only // The message is for a specific supernode only
if (user_flo_id !== "" && user_flo_id.length > 0) { if (user_flo_id !== "" && user_flo_id.length > 0) {
try { try {
@ -30193,6 +30211,7 @@
return; return;
} }
wsConn.send(finalMessage); wsConn.send(finalMessage);
} else if (user_flo_id === localbitcoinplusplus.wallets.my_local_flo_address) { } else if (user_flo_id === localbitcoinplusplus.wallets.my_local_flo_address) {
websocket.send(finalMessage); websocket.send(finalMessage);
} }