-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscripts.js
96 lines (78 loc) · 2.59 KB
/
scripts.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
89
90
91
92
93
94
95
96
const lighthouseRows = 14;
const lighthouseCols = 28;
const xScale = 14;
const yScale = 2 * xScale;
let connection = null;
let auth = null;
let display = null;
function updateDisplay(rgb) {
const ctx = display.getContext("2d");
for (let i = 0; i < (rgb.length / 3); i++) {
const r = rgb[3 * i];
const g = rgb[3 * i + 1];
const b = rgb[3 * i + 2];
const y = Math.floor(i / lighthouseCols);
const x = i % lighthouseCols;
ctx.fillStyle = `rgb(${r},${g},${b})`;
ctx.fillRect(x * xScale, y * yScale, xScale, yScale);
}
}
function setUpDisplay() {
display = document.getElementById("display");
display.width = xScale * lighthouseCols;
display.height = yScale * lighthouseRows;
updateDisplay(new Uint8Array(3 * lighthouseRows * lighthouseCols));
}
function setUpConnection() {
connection = new WebSocket(`${location.origin.replace(/^http/, "ws")}/websocket`);
connection.binaryType = "arraybuffer";
connection.addEventListener("open", () => {
console.log("Connected!");
});
connection.addEventListener("message", event => {
try {
const message = MessagePack.decode(new Uint8Array(event.data));
if (message.PAYL instanceof Uint8Array) {
updateDisplay(message.PAYL);
} else {
console.log(`Something else: ${message.PAYL instanceof Uint8Array}`);
}
} catch (e) {
console.log(`Error while decoding message from WebSocket: ${e}`);
}
});
}
function setUpFormListener() {
const form = document.getElementById("auth-form");
const fieldset = document.getElementById("auth-form-fieldset");
const usernameField = document.getElementById("username");
const tokenField = document.getElementById("token");
form.addEventListener("submit", event => {
event.preventDefault();
if (auth) {
alert("You are already authenticated!");
return;
}
const username = usernameField.value;
const token = tokenField.value;
if (!username) {
alert("Please provide a username!");
return;
}
auth = { USER: username, TOKEN: token };
fieldset.disabled = true;
connection.send(MessagePack.encode({
VERB: "STREAM",
PATH: ["user", username, "model"],
AUTH: auth,
META: {},
REID: 0,
PAYL: null,
}));
});
}
window.addEventListener("load", () => {
setUpDisplay();
setUpConnection();
setUpFormListener();
});