-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
72 lines (55 loc) · 1.57 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
const express = require('express');
const app = express();
const server = require('http').createServer(app);
const io = require('socket.io')(server);
// Serve static files from the 'public' directory
app.use(express.static('public'));
// Set up EJS as the view engine
app.set('view engine', 'ejs');
// Home page route
app.get('/', (req, res) => {
res.render('index');
});
// Start the server
const port = process.env.PORT || 3000;
server.listen(port, () => {
console.log(`Server listening on port ${port}`);
});
const messages = [];
const channels = {
general: [],
tech: [],
random: [],
// Add more channels as needed
};
// Add this function in app.js
function resetChannels() {
channels.general = [];
channels.tech = [];
channels.random = [];
// Reset other channels as needed
}
// Export the function along with the server
module.exports = { server, resetChannels };
io.on('connection', (socket) => {
console.log('User connected');
// Handle user joining a channel
socket.on('join channel', (channel) => {
socket.join(channel);
socket.emit('message history', channels[channel]);
});
// Broadcast message to all clients in the channel
socket.on('chat message', (data) => {
const { channel, msg } = data;
channels[channel].push(msg);
// Limit the number of stored messages per channel
if (channels[channel].length > 100) {
channels[channel].shift();
}
socket.to(channel).emit('chat message', msg);
});
// Handle user disconnecting
socket.on('disconnect', () => {
console.log('User disconnected');
});
});