Skip to content

Commit

Permalink
Adjust Connection Status panel, stop updating when the connection is …
Browse files Browse the repository at this point in the history
…closed
  • Loading branch information
NI committed Oct 1, 2019
1 parent b50946d commit 5e32228
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 11 deletions.
34 changes: 24 additions & 10 deletions ui/home_socketctl.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { ECHO_FAILED } from "./socket.js";
import * as history from "./history.js";

export function build(ctx) {
const connectionStatusNotConnected = "Sshwifty is not connected";
const connectionStatusNotConnected = "Sshwifty is ready to connect";
const connectionStatusConnecting =
"Connecting to Sshwifty backend server. It should only take " +
"less than a second, or two";
Expand All @@ -11,7 +11,7 @@ export function build(ctx) {
const connectionStatusConnected =
"Sshwifty has connected to it's backend server, user interface " +
"is operational";
const connectionStatusMeasurable =
const connectionStatusUnmeasurable =
"Unable to measure connection delay. The connection maybe very " +
"busy or already lost";

Expand All @@ -37,22 +37,27 @@ export function build(ctx) {
return r;
};

let inboundPerSecond = 0,
let isClosed = false,
inboundPerSecond = 0,
outboundPerSecond = 0,
trafficPreSecondNextUpdate = new Date(),
inboundPre10Seconds = 0,
outboundPre10Seconds = 0,
trafficPre10sNextUpdate = new Date(),
inboundHistory = new history.Records(buildEmptyHistory()),
outboundHistory = new history.Records(buildEmptyHistory()),
trafficSamples = 1;
trafficSamples = 0;

let delayHistory = new history.Records(buildEmptyHistory()),
delaySamples = 0,
delayPerInterval = 0;

return {
update(time) {
if (isClosed) {
return;
}

if (time >= trafficPreSecondNextUpdate) {
trafficPreSecondNextUpdate = new Date(time.getTime() + 1000);
inboundPre10Seconds += inboundPerSecond;
Expand All @@ -70,12 +75,14 @@ export function build(ctx) {
if (time >= trafficPre10sNextUpdate) {
trafficPre10sNextUpdate = new Date(time.getTime() + 10000);

inboundHistory.update(inboundPre10Seconds / trafficSamples);
outboundHistory.update(outboundPre10Seconds / trafficSamples);
if (trafficSamples > 0) {
inboundHistory.update(inboundPre10Seconds / trafficSamples);
outboundHistory.update(outboundPre10Seconds / trafficSamples);

inboundPre10Seconds = 0;
outboundPre10Seconds = 0;
trafficSamples = 1;
inboundPre10Seconds = 0;
outboundPre10Seconds = 0;
trafficSamples = 0;
}

if (delaySamples > 0) {
delayHistory.update(delayPerInterval / delaySamples);
Expand Down Expand Up @@ -104,6 +111,8 @@ export function build(ctx) {
this.status.description = connectionStatusConnecting;
},
connected() {
isClosed = false;

this.message = "??";
this.classStyle = "working";
this.windowClass = "";
Expand All @@ -118,10 +127,11 @@ export function build(ctx) {
delaySamples++;

if (delay == ECHO_FAILED) {
this.status.delay = -1;
this.message = "";
this.classStyle = "red flash";
this.windowClass = "red";
this.status.description = connectionStatusMeasurable;
this.status.description = connectionStatusUnmeasurable;

return;
}
Expand Down Expand Up @@ -154,6 +164,8 @@ export function build(ctx) {
}
},
close(e) {
isClosed = true;

ctx.connector.inputting = false;

if (e === null) {
Expand All @@ -164,6 +176,7 @@ export function build(ctx) {
return;
}

this.status.delay = -1;
this.message = "ERR";
this.classStyle = "red flash";
this.windowClass = "red";
Expand All @@ -178,6 +191,7 @@ export function build(ctx) {
this.message = "E????";
}

this.status.delay = -1;
this.classStyle = "red flash";
this.status.description = connectionStatusDisconnected + ". Error: " + e;
}
Expand Down
4 changes: 3 additions & 1 deletion ui/socket.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,9 @@ class Dial {

ws.addEventListener("error", event => {
event.toString = () => {
return "WebSocket Error (" + event.code + ")";
return (
"WebSocket Error (" + (event.code ? event.code : "Unknown") + ")"
);
};

rd.closeWithReason(event);
Expand Down
4 changes: 4 additions & 0 deletions ui/widgets/status.vue
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,10 @@ export default {
);
},
mSecondString(n) {
if (n < 0) {
return "??";
}
const bNames = ["ms", "s", "m"];
let remain = n,
nUnit = bNames[0];
Expand Down

0 comments on commit 5e32228

Please sign in to comment.