-
Notifications
You must be signed in to change notification settings - Fork 1
/
script.js
55 lines (49 loc) · 1.29 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
var socket;
var usernameInput
var chatIDInput;
var messageInput;
var chatRoom;
var dingSound;
var messages = [];
var delay = true;
function onload(){
socket = io();
usernameInput = document.getElementById("NameInput");
chatIDInput = document.getElementById("IDInput");
messageInput = document.getElementById("ComposedMessage");
chatRoom = document.getElementById("RoomID");
dingSound = document.getElementById("Ding");
socket.on("join", function(room){
chatRoom.innerHTML = "Chatroom : " + room;
})
socket.on("recieve", function(message){
console.log(message);
if (messages.length < 9){
messages.push(message);
dingSound.currentTime = 0;
dingSound.play();
}
else{
messages.shift();
messages.push(message);
}
for (i = 0; i < messages.length; i++){
document.getElementById("Message"+i).innerHTML = messages[i];
document.getElementById("Message"+i).style.color = "#303030";
}
})
}
function Connect(){
socket.emit("join", chatIDInput.value, usernameInput.value);
}
function Send(){
if (delay && messageInput.value.replace(/\s/g, "") != ""){
delay = false;
setTimeout(delayReset, 1000);
socket.emit("send", messageInput.value);
messageInput.value = "";
}
}
function delayReset(){
delay = true;
}