-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
104 lines (89 loc) · 3.8 KB
/
Copy pathscript.js
File metadata and controls
104 lines (89 loc) · 3.8 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
const socket = io();
const messages = document.getElementById('messages');
const input = document.getElementById('input');
const loading = document.getElementById('loading');
const nextButton = document.getElementById('next-button');
let chatHistory = []; // Array to store chat messages
let isConnected = false; // Track connection status
// Hide loading animation initially
loading.style.display = 'none';
input.disabled = true; // Disable input initially
// Function to display a message in the chat window
function displayMessage(text, isMine) {
const msg = document.createElement('div');
msg.textContent = text;
msg.classList.add('message');
if (isMine) {
msg.classList.add('my-message'); // Your own message on the right
} else {
msg.classList.add('stranger-message'); // Stranger's message on the left
}
messages.appendChild(msg);
messages.scrollTop = messages.scrollHeight;
}
// Handle Next button click
nextButton.addEventListener('click', () => {
loading.style.display = 'block'; // Show loading animation
input.style.display = 'none'; // Hide input field
nextButton.style.display = 'none'; // Hide Next button
clearMessages(); // Clear messages on reconnect
chatHistory = []; // Clear chat history to start fresh
isConnected = false; // Reset connection status
input.disabled = true; // Disable input
socket.emit('nextRequest'); // Emit nextRequest to the server
});
// Listen for a match event to know the user has been paired
socket.on('match', (data) => {
loading.style.display = 'none'; // Hide loading animation when connected
input.style.display = 'block'; // Show input field
input.disabled = false; // Enable input
isConnected = true; // Set connection status
// Display chat history
chatHistory.forEach(message => {
displayMessage(message.text, message.isMine);
});
// Optionally display the stranger's message if provided
if (data.message) {
displayMessage(data.message, false);
}
// Make sure the Next button is visible after a match
nextButton.style.display = 'block';
});
// Listen for incoming messages from the stranger
socket.on('message', (data) => {
const messageText = `Stranger: ${data}`;
displayMessage(messageText, false);
chatHistory.push({ text: messageText, isMine: false }); // Store in history
});
// Handle sending messages
input.addEventListener('keydown', (e) => {
if (e.key === 'Enter' && input.value.trim() !== '' && isConnected) {
const msg = input.value;
socket.emit('message', msg);
const messageText = `You: ${msg}`;
displayMessage(messageText, true); // Display your own message
chatHistory.push({ text: messageText, isMine: true }); // Store in history
input.value = '';
}
});
// Listen for end of chat
socket.on('end', (data) => {
displayMessage(data.message, false);
input.style.display = 'none'; // Hide input field until paired with new stranger
nextButton.style.display = 'block'; // Show the next button
isConnected = false; // Reset connection status
input.disabled = true; // Disable input
});
// Function to clear messages
function clearMessages() {
messages.innerHTML = ''; // Clear message history in the chat window
}
// Listen for disconnect event
socket.on('disconnect', () => {
clearMessages(); // Clear messages when disconnected
input.style.display = 'none'; // Hide input when disconnected
nextButton.style.display = 'block'; // Show the next button when disconnected
loading.style.display = 'none'; // Ensure loading is hidden on disconnect
isConnected = false; // Reset connection status
input.disabled = true; // Disable input
});