-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpeerjs.js
More file actions
81 lines (66 loc) · 2.18 KB
/
peerjs.js
File metadata and controls
81 lines (66 loc) · 2.18 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
74
75
76
77
78
79
80
81
const express = require('express');
const { ExpressPeerServer } = require('peer');
const cors = require('cors');
const path = require('path');
const fs = require('fs');
const https = require('https');
const app = express();
const PORT = process.env.PORT || 9000;
app.use(cors());
app.use(express.json());
app.use(express.static(path.join(__dirname, 'public')));
const broadcasters = new Set();
const receivers = new Set();
app.get('/', (req, res) => {
res.json({
message: 'PeerJS Signaling Server',
broadcasters: Array.from(broadcasters),
receivers: Array.from(receivers)
});
});
app.post('/register/broadcaster', (req, res) => {
const { peerId } = req.body;
broadcasters.add(peerId);
console.log(`Broadcaster registered: ${peerId}`);
res.json({ success: true, peerId, message: 'Broadcaster registered' });
});
app.post('/register/receiver', (req, res) => {
const { peerId } = req.body;
receivers.add(peerId);
console.log(`Receiver registered: ${peerId}`);
res.json({ success: true, peerId, broadcasters: Array.from(broadcasters) });
});
app.get('/broadcasters', (req, res) => {
res.json({ broadcasters: Array.from(broadcasters) });
});
app.post('/unregister', (req, res) => {
const { peerId } = req.body;
broadcasters.delete(peerId);
receivers.delete(peerId);
console.log(`Peer unregistered: ${peerId}`);
res.json({ success: true });
});
const options = {
key: fs.readFileSync(path.join(__dirname, 'storage', 'certificates', 'key.pem')),
cert: fs.readFileSync(path.join(__dirname, 'storage', 'certificates', 'cert.pem')),
};
const server = https.createServer(options, app).listen(PORT, () => {
console.log(`HTTPS Server running on port ${PORT}`);
});
const peerServer = ExpressPeerServer(server, {
debug: true,
path: '/',
allow_discovery: true
});
peerServer.on('connection', (client) => {
console.log(`Peer connected: ${client.getId()}`);
});
peerServer.on('disconnect', (client) => {
const peerId = client.getId();
console.log(`Peer disconnected: ${peerId}`);
broadcasters.delete(peerId);
receivers.delete(peerId);
});
app.use('/peerjs', peerServer);
console.log(`PeerJS server started on HTTPS port ${PORT}`);
console.log(`PeerJS path: /peerjs`);