-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
140 lines (116 loc) · 4.9 KB
/
server.js
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
const http = require('http');
const express = require('express');
const { Server } = require('socket.io');
// Create an Express application
const app = express();
const server = http.createServer(app);
const io = new Server(server);
// Serve static files from the 'public' directory
app.use(express.static('public'));
const animeNames = [
'Shinji', 'Gendo', 'Rei', 'Asuka', 'Misato',
'Yagami', 'Mikasa', 'Eren', 'Armin', 'Levi', 'Gon',
'Killua', 'Kurapika', 'Hisoka', 'Chrollo',
'Tanjiro', 'Nezuko', 'Zenitsu', 'Inosuke', 'Kanao',
'Rengoku', 'Shinobu', 'Tengen', 'Muzan', 'Ufotable',
'Yuu', 'Mitsuha', 'Taki', 'Denji', 'Makima',
'Power', 'Aki', 'Kishibe', 'Himeno', 'Kobeni'
];
const usedNames = new Set(); // Set to keep track of used names
let waitingClients = []; // Queue for clients waiting for a partner
let currentChat = {}; // Keeps track of the current chat partners
// Function to get a random anime name that is not already used
function getRandomName() {
let name;
do {
name = animeNames[Math.floor(Math.random() * animeNames.length)];
} while (usedNames.has(name) && usedNames.size < animeNames.length); // Ensure name is unique
usedNames.add(name);
return name;
}
// Handle new connections
io.on('connection', (socket) => {
console.log('A user connected:', socket.id);
// Assign a random name to the socket
const randomName = getRandomName();
socket.name = randomName;
// Store the socket for later use
waitingClients.push(socket);
// Notify the user of their assigned name
socket.emit('connected', { yourName: randomName });
// Check if there is another user to connect
if (waitingClients.length > 1) {
const otherClient = waitingClients.find(client => client.id !== socket.id);
if (otherClient) {
otherClient.emit('connected', { yourName: otherClient.name, partnerName: randomName });
socket.emit('connected', { yourName: randomName, partnerName: otherClient.name });
// Update currentChat to keep track of connected users
currentChat[socket.id] = otherClient.id;
currentChat[otherClient.id] = socket.id;
// Remove connected clients from the waiting list
waitingClients = waitingClients.filter(client => client.id !== socket.id && client.id !== otherClient.id);
}
} else {
socket.emit('waiting');
}
socket.on('sendMessage', (message) => {
// Only send messages to the partner client
const partnerId = currentChat[socket.id];
if (partnerId) {
io.to(partnerId).emit('receiveMessage', { sender: socket.name, content: message });
}
});
socket.on('skip', () => {
// Notify the other client that the current user has skipped
const partnerId = currentChat[socket.id];
if (partnerId) {
io.to(partnerId).emit('waiting');
}
// Remove the user from the current chat and waiting list
delete currentChat[socket.id];
if (partnerId) {
delete currentChat[partnerId];
}
// Re-add the skipped client to the waiting list
if (partnerId) {
waitingClients.push(io.sockets.sockets.get(partnerId));
}
waitingClients.push(socket);
socket.emit('waiting');
// Attempt to connect with a new partner
if (waitingClients.length > 1) {
const otherClient = waitingClients.find(client => client.id !== socket.id);
if (otherClient) {
otherClient.emit('connected', { yourName: otherClient.name, partnerName: randomName });
socket.emit('connected', { yourName: randomName, partnerName: otherClient.name });
currentChat[socket.id] = otherClient.id;
currentChat[otherClient.id] = socket.id;
// Remove connected clients from the waiting list
waitingClients = waitingClients.filter(client => client.id !== socket.id && client.id !== otherClient.id);
}
}
});
socket.on('disconnect', () => {
console.log('User disconnected:', socket.id);
// Remove the client from the waiting list
waitingClients = waitingClients.filter(client => client.id !== socket.id);
// Notify the partner client that the current user has disconnected
const partnerId = currentChat[socket.id];
if (partnerId) {
io.to(partnerId).emit('waiting');
delete currentChat[partnerId];
}
delete currentChat[socket.id];
// Remove the used name from the set of used names
usedNames.delete(socket.name);
});
// Heartbeat mechanism
socket.on('heartbeat', () => {
socket.emit('heartbeat_ack');
});
});
// Start the server
const PORT = process.env.PORT || 3001;
server.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});