-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.html
122 lines (103 loc) · 3.4 KB
/
test.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
<!-- public/index.html -->
<!DOCTYPE html>
<html lang="vi">
<head>
<meta charset="UTF-8">
<title>Realtime Chat với Socket.io và Phòng Chat</title>
<style>
body {
font-family: Arial, sans-serif;
}
#messages {
list-style-type: none;
padding: 0;
max-height: 300px;
overflow-y: scroll;
border: 1px solid #ddd;
margin-bottom: 10px;
}
#messages li {
padding: 8px;
border-bottom: 1px solid #ddd;
}
#form {
display: none;
flex: 1;
}
#input {
flex: 1;
padding: 8px;
}
#send {
padding: 8px;
}
.btn {
padding: 8px 12px;
margin-left: 5px;
}
</style>
</head>
<body>
<h1>Realtime Chat với Phòng Chat</h1>
<div>
<input type="text" id="roomInput" placeholder="Nhập tên phòng" />
<button class="btn" onclick="joinRoom()">Tham gia Phòng</button>
<button class="btn" onclick="leaveRoom()">Rời Phòng</button>
</div>
<ul id="messages"></ul>
<form id="form" onsubmit="sendMessage(); return false;">
<input id="input" autocomplete="off" placeholder="Nhập tin nhắn..." /><button class="btn">Gửi</button>
</form>
<script src="https://cdn.socket.io/4.0.1/socket.io.min.js"></script>
<script>
const socket = io('http://localhost:5000');
let currentRoom = '';
socket.on('connect', () => {
console.log('Đã kết nối với máy chủ');
});
socket.on('notification', (msg) => {
addMessage(`Notification: ${msg}`);
});
socket.on('joined_room', (room) => {
addMessage(`Bạn đã tham gia phòng: ${room}`);
});
socket.on('left_room', (room) => {
addMessage(`Bạn đã rời phòng: ${room}`);
});
socket.on('chat_message', (data) => {
addMessage(`${data.user}: ${data.message}`);
});
function joinRoom() {
const room = document.getElementById('roomInput').value.trim();
if (room) {
socket.emit('join_room', room);
currentRoom = room;
document.getElementById('form').style.display = 'flex';
addMessage(`Đang tham gia phòng: ${room}`);
}
}
function leaveRoom() {
if (currentRoom) {
socket.emit('leave_room', currentRoom);
addMessage(`Đang rời phòng: ${currentRoom}`);
currentRoom = '';
document.getElementById('form').style.display = 'none';
}
}
function sendMessage() {
const message = document.getElementById('input').value.trim();
if (message && currentRoom) {
socket.emit('chat_message', { room: currentRoom, message });
addMessage(`Bạn: ${message}`);
document.getElementById('input').value = '';
}
}
function addMessage(msg) {
const item = document.createElement('li');
item.textContent = msg;
document.getElementById('messages').appendChild(item);
window.scrollTo(0, document.body.scrollHeight);
}
</script>
</body>
</html>