-
Notifications
You must be signed in to change notification settings - Fork 0
/
ws.js
40 lines (34 loc) · 925 Bytes
/
ws.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
output = document.getElementById("output");
ws = new WebSocket("ws://localhost:8080/repl");
// Listen for the connection open event then call the sendMessage function
ws.onopen = function(e) {
log("Connected");
}
// Listen for the close connection event
ws.onclose = function(e) {
log("Disconnected: " + e.reason);
}
// Listen for connection errors
ws.onerror = function(e) {
log("Error ");
}
// Listen for new messages arriving at the client
ws.onmessage = function(e) {
log("Command received: " + e.data);
try {
var result = eval(e.data);
ws.send(result);
log("Result: "+result);
} catch(err) {
log(err);
}
}
// Display logging information in the document.
function log(s) {
var p = document.createElement("p");
p.style.wordWrap = "break-word";
p.textContent = s;
output.appendChild(p);
// Also log information on the javascript console
console.log(s);
}