blob: 22a2fd8b6c4110af631c040f533e157b51bbe925 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
|
/**
* Copyright (c) 2016 Weitian LI <liweitianux@live.com>
* MIT license
*
* WebSocket codes for the Web UI of "fg21sim"
*/
/**
* Global variable
* FIXME: try to avoid this ...
*/
var ws = null; /* WebSocket */
/**
* Get the full WebSocket URL from the given URI
*/
var getWebSocketURL = function (uri) {
var proto = {"http:": "ws:", "https:": "wss:"}[location.protocol];
var host = location.hostname;
var port = location.port;
var url = proto + "//" + host + ":" + port + uri;
return url;
};
/**
* Update the contents of element "#ws-status" according to the current
* WebSocket status.
*/
var updateWSStatus = function (action) {
if (action === "open") {
// WebSocket opened and connected
$("#ws-status").removeClass("label-default label-warning label-danger")
.addClass("label-success");
$("#ws-status .icon").removeClass("fa-question-circle fa-warning")
.removeClass("fa-times-circle")
.addClass("fa-check-circle");
$("#ws-status .text").text("Connected");
}
else if (action === "close") {
// WebSocket closed
$("#ws-status").removeClass("label-default label-success label-danger")
.addClass("label-warning");
$("#ws-status .icon").removeClass("fa-question-circle fa-check-circle")
.removeClass("fa-times-circle")
.addClass("fa-warning");
$("#ws-status .text").text("Disconnected!");
}
else if (action === "error") {
// WebSocket encountered error
$("#ws-status").removeClass("label-default label-success label-warning")
.addClass("label-danger");
$("#ws-status .icon").removeClass("fa-question-circle fa-check-circle")
.removeClass("fa-warning")
.addClass("fa-times-circle");
$("#ws-status .text").text("Error!!");
}
else if (action === "unsupported") {
// WebSocket NOT supported
$("#ws-status").removeClass("label-default").addClass("label-danger");
$("#ws-status .icon").removeClass("fa-question-circle")
.addClass("fa-times-circle");
$("#ws-status .text").text("Unsupported!!");
}
else {
console.error("updateWSStatus: Unknown action: ", action);
}
};
/**
* Connect to WebSocket and bind functions to events
*/
var connectWebSocket = function (url) {
ws = new WebSocket(url);
ws.onopen = function () {
console.log("Opened WebSocket: " + ws.url);
updateWSStatus("open");
};
ws.onclose = function (e) {
console.log("WebSocket closed because: ", e.reason);
updateWSStatus("close");
// Reconnect
console.log("Reconnect the WebSocket in 1 second");
setTimeout(function () { connectWebSocket(url); }, 1000);
};
ws.onerror = function (e) {
console.error("WebSocket encountered error: ", e.message);
updateWSStatus("error");
};
ws.onmessage = function (e) {
console.log("WebSocket received message:");
console.log(e.data);
};
};
$(document).ready(function () {
/**
* Check `WebSocket` support
*/
if (window.WebSocket) {
// WebSocket supported
console.log("Great, WebSocket is supported!");
var ws_url = getWebSocketURL("/ws");
connectWebSocket(ws_url);
} else {
// WebSocket NOT supported
console.error("Oops, WebSocket is NOT supported!");
updateWSStatus("unsupported");
}
});
|