-
Notifications
You must be signed in to change notification settings - Fork 1
/
client.js
54 lines (45 loc) · 1.71 KB
/
client.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
const ws = new WebSocket("ws://localhost:24642");
const message_input = document.getElementById("message_input");
const username_input = document.getElementById("username_input")
const send_button = document.getElementById("send_button");
const code_input = document.getElementById("code_input");
ws.addEventListener("open", () => {
console.log("Connected to Server!");
});
const sendMessage = async () => {
if (message_input.value !== "") {
code_hashed = await sha256(code_input.value);
ws.send(
JSON.stringify(
{ username: username_input.value,
code: code_hashed,
content: message_input.value }));
}
message_input.value="";
}
send_button.addEventListener("click", sendMessage);
const addMessage = (received) => {
const msg_table = document.getElementById("message_table");
const msg_row = msg_table.insertRow();
const user_cell = msg_row.insertCell();
const code_cell = msg_row.insertCell()
const msg_cell = msg_row.insertCell();
user_cell.setAttribute("class", "sender_cell");
code_cell.setAttribute("class", "code_cell");
msg_cell.setAttribute("class", "message_cell");
user_cell.innerHTML = received.username;
code_cell.innerHTML = received.code.substring(0, 8);
msg_cell.innerHTML = received.content;
}
ws.addEventListener("message", e => {
console.log("Received ", e.data);
const received = JSON.parse(e.data);
addMessage(received);
});
async function sha256(message) {
const msgBuffer = new TextEncoder().encode(message);
const hashBuffer = await crypto.subtle.digest('SHA-256', msgBuffer);
const hashArray = Array.from(new Uint8Array(hashBuffer));
const hashHex = hashArray.map(b => ('00' + b.toString(16)).slice(-2)).join('');
return hashHex;
}