-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
88 lines (71 loc) · 2.63 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
80
81
82
83
84
85
86
87
88
const express = require('express');
const http = require('http');
const socketIo = require('socket.io');
const { v4: uuidv4 } = require('uuid');
const path = require('path');
const cors = require('cors');
const { Buffer } = require('buffer');
const app = express();
const server = http.createServer(app);
const io = socketIo(server, {
maxHttpBufferSize: 1e8,
pingTimeout: 60000,
pingInterval: 25000
});
app.use(cors());
app.set('view engine', 'ejs');
app.set('views', path.join(__dirname, 'views'));
app.use(express.static(path.join(__dirname, 'public')));
const clients = {};
const fileChunks = {};
io.on('connection', (socket) => {
const clientId = uuidv4();
clients[clientId] = socket;
socket.emit('clientId', { clientId });
console.log(`Client connected: ${clientId}`);
socket.on('fileChunk', (data) => {
const { targetClientId, fileName, fileType, fileData, offset, lastChunk } = data;
if (!fileChunks[fileName]) {
fileChunks[fileName] = [];
}
const chunkBuffer = Buffer.from(new Uint8Array(fileData));
fileChunks[fileName][offset] = chunkBuffer;
if (lastChunk) {
try {
const sortedChunks = fileChunks[fileName].filter(chunk => chunk !== undefined);
const fileBuffer = Buffer.concat(sortedChunks);
const targetClient = clients[targetClientId];
if (targetClient) {
targetClient.emit('fileReceive', {
fileName,
fileType,
fileData: fileBuffer.toString('base64'),
senderClientId: clientId
});
socket.emit('fileStatus', { success: true, message: 'File berhasil dikirim!' });
} else {
socket.emit('fileStatus', { success: false, message: 'Target client tidak terhubung.' });
}
delete fileChunks[fileName];
} catch (error) {
console.error('Error during file reconstruction:', error);
socket.emit('fileStatus', { success: false, message: 'Terjadi kesalahan saat menggabungkan file.' });
}
}
});
socket.on('disconnect', () => {
console.log(`Client disconnected: ${clientId}`);
delete clients[clientId];
});
});
app.use((req, res, next) => {
req.domain = req.headers.host;
next();
});
app.get('/', (req, res) => {
res.render('index', { domain: req.domain });
});
const port = 3000;
server.listen(port, () => {
console.log(`Server is running on port ${port}`);
});