-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
76 lines (61 loc) · 2.37 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
// index.js
const fs = require('fs');
const path = require('path');
const https = require('https');
const express = require('express');
const socketIo = require('socket.io');
// Load self-signed certificate (or your real certificate)
const key = fs.readFileSync(path.join(__dirname, 'key.pem'));
const cert = fs.readFileSync(path.join(__dirname, 'cert.pem'));
// Create an Express app and HTTPS server
const app = express();
const server = https.createServer({ key, cert }, app);
// Attach Socket.IO to the HTTPS server
const io = socketIo(server);
// Serve static files from the "public" folder
app.use(express.static(path.join(__dirname, 'public')));
// In-memory store for connected users
const connectedUsers = {};
// In-memory chat history: stores messages for 10 minutes
let chatHistory = [];
// Periodic cleanup: remove messages older than 10 minutes (600,000 ms)
setInterval(() => {
const tenMinutesAgo = Date.now() - 10 * 60 * 1000;
// Since messages are stored in order, remove from the front until the condition is met.
while (chatHistory.length && chatHistory[0].timestamp < tenMinutesAgo) {
chatHistory.shift();
}
}, 60000); // Run every minute
io.on('connection', (socket) => {
console.log(`Socket connected: ${socket.id}`);
// Send current chat history (last 10 minutes) to the new client
socket.emit('chat history', chatHistory);
// Listen for username setup
socket.on('set username', (username) => {
connectedUsers[socket.id] = username;
console.log(`User [${socket.id}] set username: ${username}`);
});
// Listen for incoming encrypted messages
socket.on('encrypted message', (data) => {
const fromUser = connectedUsers[socket.id] || 'Anonymous';
// Append a timestamp to the message data
const messageData = {
...data,
fromUser,
timestamp: Date.now(),
};
// Store the message in chat history
chatHistory.push(messageData);
// Broadcast the message to all connected clients
io.emit('encrypted message', messageData);
});
socket.on('disconnect', () => {
console.log(`Socket disconnected: ${socket.id}`);
delete connectedUsers[socket.id];
});
});
// Start the server on port 3000, binding to all interfaces
server.listen(3000, '0.0.0.0', () => {
console.log('Secure chat server running on https://0.0.0.0:3000');
console.log('If using a self-signed cert, accept the browser warning.');
});