-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
76 lines (65 loc) · 2.34 KB
/
index.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
<html>
<head>
<script src="https://cdn.tailwindcss.com"></script>
<script src="https://cdn.socket.io/4.3.2/socket.io.min.js"
integrity="sha384-KAZ4DtjNhLChOB/hxXuKqhMLYvx3b5MlT55xPEiNmREKRzeEm+RVPlTnAn0ajQNs"
crossorigin="anonymous"></script>
<script>
const socket = io('http://localhost:8000');
socket.on('connect', function () {
const params = new URLSearchParams(window.location.search);
if (params.has('chat')) {
socket.emit('join_chat', params.get('chat'));
}
});
socket.on('chats', function (data) {
data.forEach(item => {
var link = document.createElement('div');
link.innerText = item.id;
link.className = "cursor-pointer p-2 rounded-lg text-white bg-white/10";
function onChatClicked() {
const params = new URLSearchParams(window.location.search);
params.set('chat', item.id);
window.location.href = '?' + params.toString();
}
link.addEventListener('click', onChatClicked);
document.getElementById('chats').append(link);
});
});
socket.on('messages', function (data) {
data.forEach(item => {
var link = document.createElement('div');
link.innerText = item.text;
link.className = "p-2 rounded-lg bg-blue-500 text-white self-start";
document.getElementById('messages').append(link);
});
});
function send() {
socket.emit('send_message', {
text: document.getElementById('message').value,
chat_id: new URLSearchParams(window.location.search).get('chat'),
});
document.getElementById('message').value = '';
}
</script>
</head>
<body>
<div class="h-screen p-10 bg-black">
<div class="h-full flex bg-white/10 rounded-lg">
<div class="basis-[400px] p-4">
<div class="flex flex-col gap-4" id="chats">
</div>
</div>
<div class="basis-0 grow flex flex-col gap-4 p-4">
<div class="basis-0 grow flex flex-col gap-4 overflow-scroll" id="messages">
</div>
<div class="flex gap-4">
<input id="message" type="text" placeholder="Message"
class="grow bg-white/10 py-4 px-6 text-white rounded-lg">
<button onclick="send()" class="bg-white py-2 px-8 rounded-lg text-sm">Send</button>
</div>
</div>
</div>
</div>
</body>
</html>