-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.js
More file actions
61 lines (52 loc) · 1.76 KB
/
client.js
File metadata and controls
61 lines (52 loc) · 1.76 KB
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
class Message {
constructor(type, data, user) {
this.type = type;
this.data = data;
this.to_user = user;
}
}
function ws_send(type, data, user, ws) {
client_message.type = type;
client_message.data = data;
client_message.to_user = user;
ws.send(JSON.stringify(client_message));
}
client_message = new Message();
var parsedMsg = "";
const text = document.getElementById("text_field");
const text_label = document.getElementById("text_label");
const username = document.getElementById("username");
const user_label = document.getElementById("user_label");
const sendto_username = document.getElementById("sendto_username");
const sendto_user_label = document.getElementById("sendto_user_label");
sendto_username.addEventListener("keydown", (e) => {
let kc = e.which || e.keyCode;
if (kc === 13) {
const ws = new WebSocket("wss://ws.alloapp.io:8080/");
const user_id = username.value;
const sendto_user_id = sendto_username.value;
username.style.display = "none";
user_label.style.display = "none";
sendto_username.style.display = "none";
sendto_user_label.style.display = "none";
text.style.display = "block";
text_label.style.display = "block";
ws.addEventListener("open", () => {
text.addEventListener("keydown", (e) => {
let kc = e.which || e.keyCode;
if (kc === 13) {
ws_send("message", text.value, sendto_user_id, ws);
text.value = "";
}
});
ws_send("username", user_id, "", ws);
});
ws.addEventListener("message", (e) => {
parsedMsg = JSON.parse(e.data);
var para = document.createElement("p");
var node = document.createTextNode(parsedMsg.data);
para.appendChild(node);
document.body.appendChild(para);
});
}
});