This repository has been archived by the owner on Jul 26, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchat.html
54 lines (48 loc) · 1.66 KB
/
chat.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Socket Test</title>
</head>
<body>
Room ID<input id="room_id" type="text"><button id="tekan" type="button">Join</button><br>
<textarea id="text" rows="1" cols="41"></textarea><button id="pencet" type="button">Send</button>
<button id="main-main" type="button">Main-Main</button>
<h1>Haaa mari kita chat</h1>
<ul id="chat"></ul>
<script src="/socket.io/socket.io.js"></script>
<script>
let pencet = document.getElementById('pencet');
let text = document.getElementById('text');
let roomId = document.getElementById('room_id');
let chat = document.getElementById('chat');
let tekan = document.getElementById('tekan');
let mainMain = document.getElementById('main-main');
const socket = io();
let shouldConnect = true;
tekan.onclick = () => {
if (shouldConnect) {
socket.emit('join room', roomId.value);
tekan.innerText = "Disconnect";
} else {
socket.emit('disconnect from room');
tekan.innerText = "Join";
}
shouldConnect = !shouldConnect;
};
pencet.onclick = () => {
socket.emit('message', text.value);
text.value = "";
};
mainMain.onclick = () => {
socket.emit('discussion', "Discuss");
};
socket.on('message', (msg) => {
chat.innerHTML += "<p>" + msg + "</p>"
});
socket.on('discussion', (msg) => {
chat.innerHTML += '<p>' + msg + '</p>';
});
</script>
</body>
</html>