-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
76 lines (62 loc) · 2.56 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
const express = require('express');
const http = require('http'); // Change this line
const socketIo = require('socket.io');
const cors = require('cors');
const rateLimit = require('express-rate-limit');
const Filter = require('bad-words');
const app = express();
const server = http.createServer(app); // Change this line
const io = socketIo(server, {
cors: {
origin: "*", // Allow one origin
methods: ["GET", "POST"]
}
});
const limiter = rateLimit({
windowMs: 5 * 60 * 1000, // 15 minutes
max: 100, // limit each IP to 100 requests per windowMs
});
const filter = new Filter();
const PORT = process.env.PORT || 4001;
let userEmojis = {};
let userPages = {};
app.use(cors());
app.use(limiter); // Apply rate limiting
app.use(cors()); // Enable CORS for all routes
io.on('connection', (socket) => {
console.log('New client connected');
userEmojis[socket.id] = "😀"; // Set default emoji for new connections
socket.on('pageChange', (data) => {
const { page } = data;
if (userPages[socket.id]) {
socket.leave(userPages[socket.id]); // Leave the old room
}
userPages[socket.id] = page; // Update page
socket.join(page); // Join the new room
});
socket.on('emojiUpdate', (data) => {
const { emoji } = data;
userEmojis[socket.id] = emoji; // Update emoji
const page = userPages[socket.id]; // Get page of sender
socket.broadcast.to(page).emit('emojiUpdate', { userId: socket.id, emoji }); // Broadcast update
});
socket.on('cursorMove', (data) => {
const { x, y, emoji, chatMessage, chatActive } = data;
const page = userPages[socket.id]; // Get page of sender
// Broadcast to all other users on the same page
socket.broadcast.to(page).emit('cursorMove', { userId: socket.id, x, y, emoji, chatMessage, chatActive });
});
socket.on('newMessage', (data) => {
const cleanMessage = filter.clean(data.message); // Filter out bad words
// Log the message for auditing purposes
console.log(`${socket.id} sent: ${cleanMessage}`);
// ... rest of your code to handle the message
});
socket.on('disconnect', () => {
console.log('Client disconnected');
delete userEmojis[socket.id]; // Remove emoji association on disconnect
delete userPages[socket.id]; // Remove page association on disconnect
io.emit('cursorLeave', { id: socket.id });
});
});
server.listen(PORT, () => console.log(`Listening on port ${PORT}`)); // Updated port assignment