Skip to content

Commit 39be32a

Browse files
committed
feat: add machine live indicator
1 parent 8fbf425 commit 39be32a

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

views/machine.jade

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ block title
55

66
block content
77
h1 #[span.mega-octicon.octicon-device-desktop] Machine: #{machine.hostname} #[small.text-muted (#{machine._id})]
8+
h2 Status #[span#machine-status.mega-octicon.octicon-device-desktop]
89
h2 Specifications
910
dl.dl-horizontal
1011
dt.col-sm-3 Hostname
@@ -29,3 +30,38 @@ block content
2930
ul.list-unstyled
3031
- each project in projects
3132
li #[a(href="/projects/" + project._id) #{project.name}]
33+
34+
block scripts
35+
script.
36+
var host = "#{machine.address}".replace(/^http/, 'ws'); // WebSocket server address
37+
var ws = null; // WebSocket
38+
var $status = $("#machine-status"); // Machine status icon
39+
40+
// Connects to WebSocket server
41+
var wsConnect = function() {
42+
ws = new WebSocket(host); // Attempt to connect
43+
44+
// Set server status as on
45+
ws.onopen = function(event) {
46+
$status.removeClass("text-danger");
47+
$status.addClass("text-success");
48+
};
49+
// Set server status as off
50+
ws.onclose = function(event) {
51+
// Set status
52+
$status.removeClass("text-success");
53+
$status.addClass("text-danger");
54+
};
55+
};
56+
57+
// Reconnects to WebSocket server if needed
58+
var wsReconnect = function() {
59+
if (!ws || ws.readyState === WebSocket.CLOSED) {
60+
wsConnect();
61+
}
62+
};
63+
64+
// Connect
65+
wsConnect();
66+
// Attempt to reconnect every 5s if necessary
67+
setInterval(wsReconnect, 5000);

0 commit comments

Comments
 (0)