forked from taranjeetsingh9/PetConnectBackend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchat-test.html
More file actions
166 lines (140 loc) · 5.55 KB
/
chat-test.html
File metadata and controls
166 lines (140 loc) · 5.55 KB
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
<!DOCTYPE html>
<html>
<head>
<title>Chat System Test</title>
<script src="https://cdn.socket.io/4.7.2/socket.io.min.js"></script>
<style>
body { font-family: Arial; margin: 20px; }
#chat { border: 1px solid #ccc; padding: 10px; height: 300px; overflow-y: scroll; margin-bottom: 10px; }
#messageInput { width: 70%; padding: 8px; }
button { padding: 8px 15px; margin: 5px; }
.message { margin: 5px 0; padding: 8px; background: #f0f0f0; border-radius: 5px; }
.typing { font-style: italic; color: #666; }
</style>
</head>
<body>
<h1>🔧 Chat System Live Test</h1>
<div>
<label>Token: </label>
<input type="text" id="tokenInput" style="width: 500px;"
value="eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyIjp7ImlkIjoiNjhkZDUyYjdkMzZjOGY3MGIyNTAwZGYxIiwicm9sZSI6InN0YWZmIiwib3JnYW5pemF0aW9uIjoiNjhkZDUyYjZkMzZjOGY3MGIyNTAwZGVlIn0sImlhdCI6MTc2MDg0ODk0MywiZXhwIjoxNzYwODUyNTQzfQ.FlNx7ElNC8qMxkIWhicuCjjdwlKHyl0UomcG9j98mbY" />
<button onclick="connectSocket()">Connect</button>
<button onclick="joinChat()">Join Chat</button>
</div>
<div id="status">Status: Disconnected</div>
<div id="chat"></div>
<div>
<input type="text" id="messageInput" placeholder="Type your message..."
onkeydown="handleTyping()" onkeyup="stopTyping()" />
<button onclick="sendMessage()">Send Message</button>
<button onclick="testTyping()">Test Typing</button>
</div>
<script>
let socket;
const CHAT_ID = "68f3091f93aedc021de79be8"; // Your chat ID
function connectSocket() {
const token = document.getElementById('tokenInput').value;
socket = io('http://localhost:5001', {
auth: { token: token }
});
socket.on('connect', () => {
updateStatus('✅ Connected to server');
logMessage('System', 'Connected to WebSocket server');
});
socket.on('welcome', (data) => {
logMessage('System', data.message);
});
socket.on('new-message', (message) => {
logMessage(message.sender?.name || 'User', message.content);
});
socket.on('user-typing', (data) => {
showTyping(data.userName);
});
socket.on('user-stop-typing', (data) => {
hideTyping();
});
socket.on('user-joined-chat', (data) => {
logMessage('System', `User ${data.userId} joined the chat`);
});
socket.on('error', (error) => {
logMessage('System', `Error: ${error.message}`);
});
socket.on('disconnect', () => {
updateStatus('❌ Disconnected');
});
}
function joinChat() {
if (socket) {
socket.emit('join-chat', CHAT_ID);
logMessage('System', `Joined chat room: ${CHAT_ID}`);
}
}
function sendMessage() {
const input = document.getElementById('messageInput');
const message = input.value.trim();
if (message && socket) {
socket.emit('send-message', {
chatId: CHAT_ID,
content: message,
messageType: 'text'
});
input.value = '';
}
}
let typingTimer;
function handleTyping() {
if (socket) {
socket.emit('typing-start', { chatId: CHAT_ID });
clearTimeout(typingTimer);
typingTimer = setTimeout(stopTyping, 1000);
}
}
function stopTyping() {
if (socket) {
socket.emit('typing-stop', { chatId: CHAT_ID });
}
}
function testTyping() {
if (socket) {
socket.emit('typing-start', { chatId: CHAT_ID });
setTimeout(() => {
socket.emit('typing-stop', { chatId: CHAT_ID });
}, 2000);
}
}
function updateStatus(message) {
document.getElementById('status').textContent = `Status: ${message}`;
}
function logMessage(sender, content) {
const chat = document.getElementById('chat');
const messageDiv = document.createElement('div');
messageDiv.className = 'message';
messageDiv.innerHTML = `<strong>${sender}:</strong> ${content}`;
chat.appendChild(messageDiv);
chat.scrollTop = chat.scrollHeight;
}
function showTyping(userName) {
let typingDiv = document.getElementById('typing-indicator');
if (!typingDiv) {
typingDiv = document.createElement('div');
typingDiv.id = 'typing-indicator';
typingDiv.className = 'typing';
document.getElementById('chat').appendChild(typingDiv);
}
typingDiv.textContent = `${userName} is typing...`;
}
function hideTyping() {
const typingDiv = document.getElementById('typing-indicator');
if (typingDiv) {
typingDiv.remove();
}
}
// Enter key to send message
document.getElementById('messageInput').addEventListener('keypress', (e) => {
if (e.key === 'Enter') {
sendMessage();
}
});
</script>
</body>
</html>