-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
79 lines (65 loc) · 1.99 KB
/
script.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
function addMessage (msg, target) {
var p = document.createElement("p");
p.textContent = msg;
target.insertBefore(p, target.firstChild);
};
function addImage (from, src, target) {
var img = new Image();
img.onload = function () {
var p = document.createElement("p");
p.textContent = from;
target.insertBefore(img, target.firstChild);
target.insertBefore(p, target.firstChild);
};
img.src = src;
};
function onSubmit (socket, type) {
var myName = document.getElementById("myName").textContent;
var input = document.getElementById("message_field");
var target = document.getElementById("message_box");
if (type === "text") {
addMessage(myName + ": " + input.value, target);
} else if (type === "image") {
addImage(myName + ": ", input.value, target);
}
socket.send(JSON.stringify({
type: type,
from: myName,
content: input.value
}));
input.value = null;
};
function onMessage (rawMessage) {
console.log(rawMessage.data);
try {
var message = JSON.parse(rawMessage.data);
} catch (error) {
console.error(error);
return;
}
var messageBox = document.getElementById("message_box");
if (message.type === "text" && message.content.length < 40) {
addMessage(message.from + ": " + message.content, messageBox);
} else if (message.type === "image") {
addImage(message.from + ":", message.content, messageBox);
}
};
function onClose () {
console.log("socket closed");
};
document.addEventListener("DOMContentLoaded", function () {
var socket = new WebSocket("ws://172.22.131.203:3000");
socket.onmessage = onMessage;
socket.onclose = onClose;
document.getElementById("message_submit")
.addEventListener("click", function () {
onSubmit(socket, "text");
});
document.getElementById("image_submit")
.addEventListener("click", function () {
onSubmit(socket, "image");
});
window.addEventListener("beforeunload", function () {
socket.close();
});
});