-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
73 lines (60 loc) · 2.05 KB
/
Copy pathserver.js
File metadata and controls
73 lines (60 loc) · 2.05 KB
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
import { createServer } from "http";
import { createServer as createHttpsServer } from "https";
import { WebSocketServer } from "ws";
import fs from 'fs/promises';
// 1. Création des serveurs HTTP et HTTPS
const httpsServer = createHttpsServer({
key: await fs.readFile('keys/private.key'),
cert: await fs.readFile('keys/certificate.crt')
});
// 2. Création des WebSocket Servers
const wssHttps = new WebSocketServer({ server: httpsServer });
// 3. Gestion des clients
function setupWebSocket(wss) {
const clients = [];
wss.on('connection', ws => {
clients.push(ws);
ws.on('message', message => {
clients.forEach(client => {
if (client !== ws && client.readyState === 1) { // 1 = OPEN
client.send(message.toString());
}
console.log(message,client)
});
});
ws.on('close', () => {
const index = clients.indexOf(ws);
if (index !== -1) {
clients.splice(index, 1);
}
// Notifier tous les autres clients de la déconnexion
clients.forEach(client => {
if (client.readyState === 1) { // 1 = OPEN
client.send(JSON.stringify({
type: 'peer_disconnected'
}));
}
});
});
});
}
setupWebSocket(wssHttps);
// 4. Gestion des requêtes HTTP/HTTPS
async function handleRequest(req, res) {
let filePath = '.' + req.url;
if (filePath === './') filePath = './public/sender.html';
try {
const content = await fs.readFile(filePath);
res.writeHead(200);
res.end(content);
} catch (err) {
res.writeHead(404);
res.end('Fichier non trouvé');
}
}
httpsServer.on('request', handleRequest);
// 5. Démarrage des serveurs
httpsServer.listen(443, "0.0.0.0", () => {
console.log('HTTPS Server: https://localhost:443');
console.log('WebSocket Server: wss://localhost:443');
});