Skip to content

Commit e46c09f

Browse files
committed
feat: add server status indicator
1 parent 9a02ce1 commit e46c09f

File tree

7 files changed

+41
-11
lines changed

7 files changed

+41
-11
lines changed

lab.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ var favicon = require("serve-favicon");
1313
var morgan = require("morgan");
1414
var rp = require("request-promise");
1515
var Promise = require("bluebird");
16-
//var WebSocketServer = require("ws").Server;
16+
var WebSocketServer = require("ws").Server;
1717
var db = require("./db").db;
1818

1919
/* App instantiation */
@@ -636,15 +636,14 @@ if (!process.env.FGLAB_PORT) {
636636

637637
/* WebSocket server */
638638
// Add websocket server
639-
/*
640639
var wss = new WebSocketServer({server: server});
641640

642641
// Call on connection from new client
643642
wss.on("connection", function(ws) {
644-
console.log("Client opened connection");
643+
//console.log("Client opened connection");
645644

646645
// Send one message
647-
ws.send("Connected to server");
646+
//ws.send("Connected to server");
648647

649648
// Print received messages
650649
ws.on("message", function(message) {
@@ -653,7 +652,6 @@ wss.on("connection", function(ws) {
653652

654653
// Perform clean up if necessary
655654
ws.on("close", function() {
656-
console.log("Client closed connection");
655+
//console.log("Client closed connection");
657656
});
658657
});
659-
*/

views/experiment.jade

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ block content
5050
div(id= "chart" + ind)
5151

5252
block scripts
53-
script(src="/bower_components/jquery/dist/jquery.min.js")
5453
script(src="/bower_components/bootstrap/dist/js/bootstrap.min.js")
5554
script(src="/bower_components/d3/d3.min.js")
5655
script(src="/bower_components/c3/c3.min.js")

views/experiments.jade

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ block content
1111
table#experiments
1212

1313
block scripts
14-
script(src="/bower_components/jquery/dist/jquery.min.js")
1514
script(src="/bower_components/bootstrap/dist/js/bootstrap.min.js")
1615
script(src="/bower_components/bootstrap-table/dist/bootstrap-table.min.js")
1716
script(src="/bower_components/bootstrap-table/dist/extensions/multiple-sort/bootstrap-table-multiple-sort.min.js")

views/index.jade

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ block content
5353
a(href="/machines/" + machine._id) #{machine.hostname}
5454

5555
block scripts
56-
script(src="/bower_components/jquery/dist/jquery.min.js")
5756
script.
5857
$(function() {
5958
// Make home button active

views/layout.jade

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,43 @@ html(lang="en")
2121
a.nav-link(href="javascript:history.back()") #[span.octicon.octicon-chevron-left]
2222
li.nav-item
2323
a.nav-link(href="javascript:history.forward()") #[span.octicon.octicon-chevron-right]
24+
ul.nav.navbar-nav.pull-right
25+
li.nav-item
26+
a.nav-link #[span#server-status.octicon.octicon-server.text-success]
2427
.container
2528
block content
29+
script(src="/bower_components/jquery/dist/jquery.min.js")
30+
script.
31+
var host = location.origin.replace(/^http/, 'ws'); // WebSocket server address
32+
var ws = null; // WebSocket
33+
var $status = $("#server-status"); // Server status icon
34+
35+
// Connects to WebSocket server
36+
var wsConnect = function() {
37+
ws = new WebSocket(host); // Attempt to connect
38+
39+
// Set server status as on
40+
ws.onopen = function(event) {
41+
$status.removeClass("text-danger");
42+
$status.addClass("text-success");
43+
};
44+
// Set server status as off
45+
ws.onclose = function(event) {
46+
// Set status
47+
$status.removeClass("text-success");
48+
$status.addClass("text-danger");
49+
};
50+
};
51+
52+
// Reconnects to WebSocket server if needed
53+
var wsReconnect = function() {
54+
if (!ws || ws.readyState === WebSocket.CLOSED) {
55+
wsConnect();
56+
}
57+
};
58+
59+
// Connect
60+
wsConnect();
61+
// Attempt to reconnect every 5s if necessary
62+
setInterval(wsReconnect, 5000);
2663
block scripts

views/optimisation.jade

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ block content
6060
button#random-search.btn.btn-primary(type="submit") Random Search
6161

6262
block scripts
63-
script(src="/bower_components/jquery/dist/jquery.min.js")
6463
script(src="/bower_components/bootstrap/dist/js/bootstrap.min.js")
6564
script(src="/bower_components/lodash/lodash.min.js")
6665
script.

views/project.jade

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ block content
4343
button.btn.btn-primary(type="submit") Submit
4444

4545
block scripts
46-
script(src="/bower_components/jquery/dist/jquery.min.js")
4746
script(src="/bower_components/bootstrap/dist/js/bootstrap.min.js")
4847
script.
4948
// Parse from string instead prior to accessing internal objects via Jade

0 commit comments

Comments
 (0)