-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
79 lines (69 loc) · 2.16 KB
/
app.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
const express = require('express');
const http = require('http');
const { Server } = require('socket.io');
const path = require('path');
const app = express();
const server = http.createServer(app);
const io = new Server(server);
app.use(express.static(path.join(__dirname, 'public')));
app.set('view engine', 'ejs');
// Store users in memory (in a production app, you'd use a database)
const users = new Map();
app.get('/', (req, res) =>
{
res.render('index');
// res.sendFile(path.join(__dirname, 'public', 'html', 'index.html'));
});
io.on('connection', (socket) =>
{
console.log('A user connected: ' + socket.id);
// Handle user registration
socket.on('register', (username) =>
{
users.set(socket.id, { id: socket.id, username });
// Broadcast updated user list to all clients
io.emit('updateUsers', Array.from(users.values()));
});
// Handle explicit logout
socket.on('logout', () =>
{
if (users.has(socket.id))
{
users.delete(socket.id);
// Broadcast updated user list to all clients
io.emit('updateUsers', Array.from(users.values()));
console.log('User logged out: ' + socket.id);
}
});
// Handle file transfer
socket.on('sendFile', ({ receiverId, fileData, fileName, timestamp, receiverName }) =>
{
const sender = users.get(socket.id);
if (sender && users.has(receiverId))
{
io.to(receiverId).emit('receiveFile', {
fileData,
fileName,
senderId: socket.id,
senderName: sender.username,
timestamp
});
}
});
// Handle disconnection (including unexpected disconnects)
socket.on('disconnect', () =>
{
if (users.has(socket.id))
{
users.delete(socket.id);
// Broadcast updated user list to all clients
io.emit('updateUsers', Array.from(users.values()));
console.log('User disconnected: ' + socket.id);
}
});
});
const PORT = 3000;
server.listen(PORT, () =>
{
console.log(`Server running on http://localhost:${PORT}`);
});