-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
94 lines (82 loc) · 2.94 KB
/
index.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
const express = require('express');
const app = express();
const http = require('http');
const server = http.createServer(app);
const { Server } = require("socket.io");
const io = new Server(server);
app.use(express.static(__dirname));
const connectedUsers = {};
app.get('/', (req, res) => {
res.sendFile(__dirname + '/index.html');
});
io.on('connection', (socket) => {
console.log('a user connected:', socket.id);
socket.on('setNickname', (nickname) => {
const existingUser = Object.values(connectedUsers).find((user) => user.nickname === nickname);
if (!existingUser) {
connectedUsers[socket.id] = { nickname, socket };
io.emit('user connected', nickname);
// Emit the list of active users only if the nickname is not already present
const activeUsers = Object.values(connectedUsers).map(user => user.nickname);
io.emit('active users', activeUsers);
}
else {
socket.emit('nickname already exists', nickname);
}
});
socket.on('useNickname', (nickname) => {
const existingUserSocketId = Object.keys(connectedUsers).find(socketId => connectedUsers[socketId].nickname === nickname);
if (existingUserSocketId) {
delete connectedUsers[existingUserSocketId];
}
connectedUsers[socket.id] = { nickname, socket };
io.emit('user connected', nickname);
const activeUsers = Object.values(connectedUsers).map(user => user.nickname);
io.emit('active users', activeUsers);
});
socket.on('chat message', (msg, recipient) => {
console.log('message: ' + msg);
const senderNickname = connectedUsers[socket.id].nickname;
if (recipient) {
const recipientSocket = Object.values(connectedUsers).find(
(user) => user.nickname === recipient
);
if (recipientSocket) {
recipientSocket.socket.emit('private message', {
nickname: senderNickname,
msg: msg,
recipient: recipient,
});
socket.emit('private message', {
nickname: senderNickname,
msg: msg,
recipient: recipient,
});
} else {
// Notify the sender that the recipient is not connected
socket.emit('private message', {
nickname: 'Server',
msg: `User "${recipient}" not found or not connected.`,
recipient: senderNickname,
});
}
} else {
io.emit('chat message', { nickname: senderNickname, msg: msg });
}
});
socket.on('disconnect', () => {
const user = connectedUsers[socket.id];
if (user) {
io.emit('user disconnected', user.nickname);
delete connectedUsers[socket.id];
const activeUsers = Object.values(connectedUsers).map(user => user.nickname);
io.emit('active users', activeUsers);
console.log('user disconnected:', socket.id);
}
});
});
const port = 3000;
const ipAddress = '192.168.1.11';
server.listen(port, ipAddress, () => {
console.log(`Server running on http://${ipAddress}:${port}`);
});