-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
88 lines (77 loc) · 2.84 KB
/
main.js
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
/* WebSocket stuff */
const wsUri = "ws://localhost:8000";
let socket;
let doSend;
const display = $("#display");
/* Objects/Data stuff */
const objects = {};
/* Action handlers */
const outputTbody = $("#output tbody");
$("#sendMsg").on("click", () => { let what; if ((what = $("#textbox").val()).length) { socket.send(what); } });
$("#clearMsg").on("click", () => { $("#textbox").val(""); });
$("#clearLog").on("click", () => { outputTbody.empty(); });
function getStatusClassFromStatus(status)
{
switch (status)
{
case "":
return "";
default:
return 'default';
}
}
function addToDisplay(data)
{
let target = (data.parent && data.parent.length) ? display.find(`#${data.parent} .exec-children`) : display;
const statusClass = getStatusClassFromStatus(data.status);
const statusDetails = data.statusDetails ? (` (${data.statusDetails})`) : "";
const template = $(`<div class="exec-wrapper" id="${data.uuid}">
<div class="panel exec-panel panel-${statusClass}">
<div class="panel-heading"><b>${data.protocol}</b> | <tt>${data.uuid}</tt></div>
<div class="panel-body">
<b>Request: ${data.reqname}</b><br>
<b>${data.reqargs}</b><br>
<b>Try #${data.tryNum}</b>
</div>
<div class="panel-footer">${data.status}${statusDetails}</div>
</div>
<div class="exec-children" style="display: none;"></div>
</div>`);
target.show();
if ($("#"+data.uuid).length) {
// replace
} else {
// create
target.append(template);
}
}
/* Let's go */
$(function()
{
function writeToLog(message, type)
{
outputTbody.append(`<tr class="type-${type}"><td>${new Date().toISOString().replace(/[ZT]/g, ' ')}</td>
<td>${type}</td>
<td><pre>${message}</pre></td></tr>`);
}
function setStatus(msg, color)
{
$("#ws_status").html(`<span style="color: ${color};"><b>${msg}</b></span>`);
}
function onMessage(evt)
{
console.log("onMessage", evt);
writeToLog(evt.data, "RECEIVED");
}
doSend = function(msg)
{
writeToLog(msg, "SENT");
socket.send(msg);
};
$("#ws_server").text(wsUri);
socket = new WebSocket(wsUri, 'echo');
socket.onopen = (evt) => { setStatus('OK', 'green'); };
socket.onerror = (evt) => { setStatus('error', 'red'); };
socket.onclose = (evt) => { setStatus('closed', 'black'); };
socket.onmessage = (msg) => { onMessage(msg); };
});