forked from mekazombify/Hack4Good2018
-
Notifications
You must be signed in to change notification settings - Fork 0
/
chat.js
59 lines (50 loc) · 1.36 KB
/
chat.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
/*
Text Chat JS
Frank Giddens
November 2, 2018
*/
"use strict";
let socket = io.connect();
let other;
let connected = false;
let messages = [];
let updateMessages = function(){
let newCard = "";
newCard += "<div class=\"card w-75 p-3\"><p class=\"card-body\">";
newCard += messages[(messages.length - 1)].mes;
newCard += "</p><div class=\"card-footer text-muted\">";
if(messages[(messages.length - 1)].user == 0){
newCard += "You";
}
else{
newCard += "Angel";
}
newCard += "</div></div><br/>";
document.getElementById("chatBox").innerHTML += newCard;
};
let sendMessage = function(){
if(connected){
messages[messages.length] = {"mes" : document.getElementById("messageBox").value, "user" : 0};
socket.emit("message", {"mes" : document.getElementById("messageBox").value, "target" : other});
document.getElementById("messageBox").value = "";
updateMessages();
}
};
socket.emit("typeCheck", {"userType" : "PIN"});
socket.on("conn", function(data){
other = data.other;
connected = true;
document.getElementById("chatBox").innerHTML = "";
});
socket.on("message", function(data){
messages[messages.length] = {"mes" : data.mes, "user" : 1};
updateMessages();
});
document.getElementById("sendMessage").addEventListener("onClick", function(){
sendMessage();
});
document.addEventListener("keyup", function(e){
if(e.keyCode == 13){
sendMessage();
}
});