-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
101 lines (82 loc) · 3.52 KB
/
index.js
File metadata and controls
101 lines (82 loc) · 3.52 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
const express = require('express');
const http = require('http');
const socketIo = require('socket.io');
const moment = require('moment'); // For handling message timestamps
// Create an Express app and HTTP server
const app = express();
const server = http.createServer(app);
const io = socketIo(server);
// Store users in memory (no database)
let users = {};
let admins = []; // List of admin socket IDs
// Serve static files (for client-side HTML, CSS, JS)
app.use(express.static('client'));
// Serve the main HTML page
app.get('/', (req, res) => {
res.sendFile(__dirname + '/client/index.html');
});
// Handle socket connections
io.on('connection', (socket) => {
console.log('A user connected: ' + socket.id);
// Handle a new user joining the chat
socket.on('new-user-joined', (name) => {
users[socket.id] = { name, isAdmin: false }; // Store user with their socket ID
console.log(`${name} joined the chat`);
// Broadcast that the user has joined
socket.broadcast.emit('user-joined', name);
// Update user count and send user list to all clients
io.emit('user-count', Object.keys(users).length);
io.emit('user-list', Object.values(users)); // Send user list
// Automatically assign admin for the first user
if (Object.keys(users).length === 1) {
users[socket.id].isAdmin = true;
admins.push(socket.id); // Mark first user as admin
}
io.emit('user-list', Object.values(users)); // Update user list
});
// Handle sending a message to everyone in the chat
socket.on('send', (message) => {
// Add timestamp to the message
const timestamp = moment().format('HH:mm');
// Broadcast the message to all clients except the sender
socket.broadcast.emit('receive', {
message,
name: users[socket.id].name,
timestamp,
});
});
// Handle sending a private message to a specific user
socket.on('private-message', (recipientName, message) => {
// Find recipient by name
const recipient = Object.values(users).find(user => user.name === recipientName);
if (recipient) {
// Emit private message to the recipient if they exist
const timestamp = moment().format('HH:mm');
socket.to(Object.keys(users).find(id => users[id].name === recipientName))
.emit('receive-private', { from: users[socket.id].name, message, timestamp });
console.log(`Private message sent to ${recipientName}`);
} else {
console.log(`Recipient ${recipientName} not found`);
}
});
// Handle typing indicator
socket.on('typing', (isTyping) => {
if (users[socket.id]) { // Ensure the user exists before accessing their name
socket.broadcast.emit('typing', { name: users[socket.id].name, isTyping });
}
});
// Handle user disconnection
socket.on('disconnect', () => {
console.log('User disconnected: ' + socket.id);
if (users[socket.id]) {
socket.broadcast.emit('left', users[socket.id].name);
delete users[socket.id];
io.emit('user-count', Object.keys(users).length);
io.emit('user-list', Object.values(users)); // Send updated user list
}
});
});
// Start the server
server.listen(8000, () => {
console.log('Server running on http://localhost:8000');
});