-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchat.html
138 lines (122 loc) · 4.09 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>SocketChat</title>
<style>
* {
color-scheme: dark;
font-family: monospace;
scroll-behavior: smooth;
}
a { color: white; }
h1 { margin: .5em; }
body {
display: flex;
flex-direction: column;
align-items: center;
}
#form {
position: fixed;
bottom: 1em;
left: 1em;
right: 1em;
height: 3em;
display: grid;
gap: 1em;
grid-template-columns: 1fr 5em;
}
#messages {
width: 35em;
max-width: 100%;
overflow-x: hidden;
margin-bottom: 5em;
}
.message { padding: .2em; }
.message:nth-child(odd) { background-color: #4d4d4d; }
.message:nth-child(even) { background-color: #353535; }
</style>
</head>
<body>
<h1><a href="https://github.com/SX-9/socket-chat">SocketChat</a></h1>
<p>
Username: <span id="username"></span> (<span id="id"></span>)
<br>Room: <span id="roomed"></span>
<br>(Note: Rooms are not private, Every message is broadcasted to all clients but rooms filter messages)
</p>
<div id="messages"></div>
<form id="form">
<input type="text" id="message" placeholder="Type Message Here" autocomplete="off" autofocus>
<button type="submit">Send</button>
</form>
<script src="https://cdn.socket.io/4.6.0/socket.io.min.js" integrity="sha384-c79GN5VsunZvi+Q/WObgk2in0CbZsHnjEqvFxC5DxHn9lTfNce2WW6h2pH6u/kF+" crossorigin="anonymous"></script>
<script>
const params = new URLSearchParams(location.search);
const room = params.get('room') || prompt('Enter Room Id To Join:', 'default') || 'default';
const user = params.get('username') || prompt('Enter Username:');
const socket = io({ closeOnBeforeunload: false });
let silent = 0;
socket.onAny(console.log)
socket.on('connect', () => {
username.innerText = user;
roomed.innerText = room;
id.innerText = socket.id;
socket.emit('join', {
name: user || socket.id,
room,
});
});
socket.onAny((event, data) => {
if (data.room !== room) return;
displayMsg({
...data,
type: event,
});
window.scrollTo(0, document.body.scrollHeight);
});
form.onsubmit = (e) => {
e.preventDefault();
if (silent) return;
silent++;
setTimeout(() => silent--, 1000);
let msg = message.value;
if (msg.length <= 0) return;
socket.emit('message', {
room,
value: msg,
name: user || socket.id,
});
message.value = '';
};
window.onbeforeunload = (e) => {
e.preventDefault();
e.returnValue = " ";
socket.emit('leave', {
name: user || socket.id,
room,
});
};
function displayMsg(data) {
let div = document.createElement('div');
let n = data.name === name ? 'You' : data.name;
div.classList.add('message');
switch (data.type) {
case 'join':
div.innerHTML = `${n} Joined The Room!`;
div.style.color = '#99f492';
break;
case 'leave':
div.innerHTML = `${n} Left The Room!`;
div.style.color = '#f49492';
break;
case 'message':
div.innerText = `${n}: ${data.value}`;
break;
}
messages.appendChild(div);
}
</script>
</body>
</html>