Adjust Connection Status panel, stop updating when the connection is closed

This commit is contained in:
NI
2019-10-01 15:45:28 +08:00
parent b50946d8cd
commit 5e32228960
3 changed files with 31 additions and 11 deletions

View File

@@ -2,7 +2,7 @@ import { ECHO_FAILED } from "./socket.js";
import * as history from "./history.js"; import * as history from "./history.js";
export function build(ctx) { export function build(ctx) {
const connectionStatusNotConnected = "Sshwifty is not connected"; const connectionStatusNotConnected = "Sshwifty is ready to connect";
const connectionStatusConnecting = const connectionStatusConnecting =
"Connecting to Sshwifty backend server. It should only take " + "Connecting to Sshwifty backend server. It should only take " +
"less than a second, or two"; "less than a second, or two";
@@ -11,7 +11,7 @@ export function build(ctx) {
const connectionStatusConnected = const connectionStatusConnected =
"Sshwifty has connected to it's backend server, user interface " + "Sshwifty has connected to it's backend server, user interface " +
"is operational"; "is operational";
const connectionStatusMeasurable = const connectionStatusUnmeasurable =
"Unable to measure connection delay. The connection maybe very " + "Unable to measure connection delay. The connection maybe very " +
"busy or already lost"; "busy or already lost";
@@ -37,7 +37,8 @@ export function build(ctx) {
return r; return r;
}; };
let inboundPerSecond = 0, let isClosed = false,
inboundPerSecond = 0,
outboundPerSecond = 0, outboundPerSecond = 0,
trafficPreSecondNextUpdate = new Date(), trafficPreSecondNextUpdate = new Date(),
inboundPre10Seconds = 0, inboundPre10Seconds = 0,
@@ -45,7 +46,7 @@ export function build(ctx) {
trafficPre10sNextUpdate = new Date(), trafficPre10sNextUpdate = new Date(),
inboundHistory = new history.Records(buildEmptyHistory()), inboundHistory = new history.Records(buildEmptyHistory()),
outboundHistory = new history.Records(buildEmptyHistory()), outboundHistory = new history.Records(buildEmptyHistory()),
trafficSamples = 1; trafficSamples = 0;
let delayHistory = new history.Records(buildEmptyHistory()), let delayHistory = new history.Records(buildEmptyHistory()),
delaySamples = 0, delaySamples = 0,
@@ -53,6 +54,10 @@ export function build(ctx) {
return { return {
update(time) { update(time) {
if (isClosed) {
return;
}
if (time >= trafficPreSecondNextUpdate) { if (time >= trafficPreSecondNextUpdate) {
trafficPreSecondNextUpdate = new Date(time.getTime() + 1000); trafficPreSecondNextUpdate = new Date(time.getTime() + 1000);
inboundPre10Seconds += inboundPerSecond; inboundPre10Seconds += inboundPerSecond;
@@ -70,12 +75,14 @@ export function build(ctx) {
if (time >= trafficPre10sNextUpdate) { if (time >= trafficPre10sNextUpdate) {
trafficPre10sNextUpdate = new Date(time.getTime() + 10000); trafficPre10sNextUpdate = new Date(time.getTime() + 10000);
inboundHistory.update(inboundPre10Seconds / trafficSamples); if (trafficSamples > 0) {
outboundHistory.update(outboundPre10Seconds / trafficSamples); inboundHistory.update(inboundPre10Seconds / trafficSamples);
outboundHistory.update(outboundPre10Seconds / trafficSamples);
inboundPre10Seconds = 0; inboundPre10Seconds = 0;
outboundPre10Seconds = 0; outboundPre10Seconds = 0;
trafficSamples = 1; trafficSamples = 0;
}
if (delaySamples > 0) { if (delaySamples > 0) {
delayHistory.update(delayPerInterval / delaySamples); delayHistory.update(delayPerInterval / delaySamples);
@@ -104,6 +111,8 @@ export function build(ctx) {
this.status.description = connectionStatusConnecting; this.status.description = connectionStatusConnecting;
}, },
connected() { connected() {
isClosed = false;
this.message = "??"; this.message = "??";
this.classStyle = "working"; this.classStyle = "working";
this.windowClass = ""; this.windowClass = "";
@@ -118,10 +127,11 @@ export function build(ctx) {
delaySamples++; delaySamples++;
if (delay == ECHO_FAILED) { if (delay == ECHO_FAILED) {
this.status.delay = -1;
this.message = ""; this.message = "";
this.classStyle = "red flash"; this.classStyle = "red flash";
this.windowClass = "red"; this.windowClass = "red";
this.status.description = connectionStatusMeasurable; this.status.description = connectionStatusUnmeasurable;
return; return;
} }
@@ -154,6 +164,8 @@ export function build(ctx) {
} }
}, },
close(e) { close(e) {
isClosed = true;
ctx.connector.inputting = false; ctx.connector.inputting = false;
if (e === null) { if (e === null) {
@@ -164,6 +176,7 @@ export function build(ctx) {
return; return;
} }
this.status.delay = -1;
this.message = "ERR"; this.message = "ERR";
this.classStyle = "red flash"; this.classStyle = "red flash";
this.windowClass = "red"; this.windowClass = "red";
@@ -178,6 +191,7 @@ export function build(ctx) {
this.message = "E????"; this.message = "E????";
} }
this.status.delay = -1;
this.classStyle = "red flash"; this.classStyle = "red flash";
this.status.description = connectionStatusDisconnected + ". Error: " + e; this.status.description = connectionStatusDisconnected + ". Error: " + e;
} }

View File

@@ -153,7 +153,9 @@ class Dial {
ws.addEventListener("error", event => { ws.addEventListener("error", event => {
event.toString = () => { event.toString = () => {
return "WebSocket Error (" + event.code + ")"; return (
"WebSocket Error (" + (event.code ? event.code : "Unknown") + ")"
);
}; };
rd.closeWithReason(event); rd.closeWithReason(event);

View File

@@ -180,6 +180,10 @@ export default {
); );
}, },
mSecondString(n) { mSecondString(n) {
if (n < 0) {
return "??";
}
const bNames = ["ms", "s", "m"]; const bNames = ["ms", "s", "m"];
let remain = n, let remain = n,
nUnit = bNames[0]; nUnit = bNames[0];