refactoring

This commit is contained in:
DieMyst 2019-06-16 11:11:50 +03:00
parent e2ac75d18f
commit 3a8374b4f2
2 changed files with 45 additions and 42 deletions

View File

@ -24,8 +24,10 @@
<section class="container"> <section class="container">
<div class="m-5"><h1>Cosmic Salmon. Hook A Fish</h1></div> <div class="m-5"><h1>Cosmic Salmon. Hook A Fish</h1></div>
<div class="input-group border-primary m-5"> <div class="input-group border-primary m-5">
<input type="text" class="form-control" placeholder="Node IP" id="node-ip" value="1"> <input type="text" class="form-control" placeholder="Node IP" id="node-ip" value="207.154.210.117">
<input type="text" class="form-control" placeholder="Binary Source" id="binary-url" value="1"> <input type="text" class="form-control" placeholder="Node Port" id="node-port" value="26657">
<input type="text" class="form-control" placeholder="Binary Source" id="binary-url" value="QmQ69JoPDaKFpPSbWvvUGRGG5E83u6nTP2hRegmCoa6aW5">
<input type="text" class="form-control" placeholder="Name" id="node-name" value="name service">
<button type="button" class="btn btn-outline-secondary" id="start-check">Start Check</button> <button type="button" class="btn btn-outline-secondary" id="start-check">Start Check</button>
</div> </div>

View File

@ -46,17 +46,21 @@ window.onload = function () {
}; };
function startCheckEvent() { function startCheckEvent() {
let nodeIpEl = document.getElementById("node-ip"); let nodeIpEl = document.getElementById("node-ip").value;
let binaryUrlEl = document.getElementById("binary-url"); let portEl = document.getElementById("node-port").value;
let binaryUrlEl = document.getElementById("binary-url").value;
let nameEl = document.getElementById("node-name").value;
let someInfo = { let someInfo = {
name: nodeIpEl.value, name: nameEl,
nodesNumber: 34, nodesNumber: 34,
lastHeight: 13, lastHeight: 13,
binaryLink: "#" binaryLink: "#"
}; };
startCheck(someInfo.name, someInfo); let createUrl = `http://localhost:8090/create/${nameEl}/${nodeIpEl}/${portEl}/${binaryUrlEl}`;
startCheck(createUrl, someInfo.name, someInfo);
} }
function appendNewZone(id, info, status) { function appendNewZone(id, info, status) {
@ -80,7 +84,6 @@ function appendNewZone(id, info, status) {
function setHeightAndWidth(el, currentHeight, lastHeight) { function setHeightAndWidth(el, currentHeight, lastHeight) {
let percent = currentHeight / lastHeight * 100; let percent = currentHeight / lastHeight * 100;
console.log("hey = " + percent);
el.style.width = `${percent}%`; el.style.width = `${percent}%`;
el.innerHTML = `Height: ${currentHeight}`; el.innerHTML = `Height: ${currentHeight}`;
} }
@ -184,51 +187,49 @@ function getProgressBar(id) {
return document.getElementById(id).getElementsByClassName("progress-bar")[0]; return document.getElementById(id).getElementsByClassName("progress-bar")[0];
} }
function startCheck(zoneId, info) { function startCheck(createUrl, zoneId, info) {
if (state[zoneId]) { if (state[zoneId]) {
return; return;
} }
var xmlHttp = new XMLHttpRequest(); fetch(createUrl).then((sa) => {
xmlHttp.open( "GET", "http://localhost:8090/create/" + zoneId, false ); // false for synchronous request let socket = new WebSocket("ws://127.0.0.1:8090/websocket/" + zoneId);
xmlHttp.send( null );
let socket = new WebSocket("ws://127.0.0.1:8090/websocket/start/file/" + zoneId); socket.onopen = function(e) {
socket.onopen = function(e) { console.log(e);
state[zoneId] = {
state[zoneId] = { info: info
info: info };
appendNewZone(zoneId, info);
}; };
appendNewZone(zoneId, info);
console.log("Websocket was opened: " + JSON.stringify(e));
};
let k = 0; let k = 0;
socket.onmessage = function(event) { socket.onmessage = function(event) {
let st = state[zoneId]; let st = state[zoneId];
let data = JSON.parse(event.data); let data = JSON.parse(event.data);
let el = getProgressBar(zoneId); let el = getProgressBar(zoneId);
k++; k++;
if (data.height) { if (data.correct) {
if (st.info.lastHeight < 1000 || k % 100 === 0) { if (st.info.lastHeight < 1000 || k % 100 === 0) {
setHeightAndWidth(el, data.height, st.info.lastHeight); setHeightAndWidth(el, data.height, st.info.lastHeight);
} else { } else {
changeHeight(el, data.height) changeHeight(el, data.height)
} }
} else { } else {
setError(zoneId); setError(zoneId);
socket.close() socket.close()
} }
}; };
socket.onclose = function(event) { socket.onclose = function(event) {
console.log("Websocket was closed. Cause: " + JSON.stringify(event)) console.log("Websocket was closed. Cause: " + JSON.stringify(event))
}; };
socket.onerror = function(error) { socket.onerror = function(error) {
console.log("Error: " + JSON.stringify(error)) console.log("Error: " + JSON.stringify(error))
}; };
});
} }