-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
61 lines (54 loc) · 1.82 KB
/
server.js
File metadata and controls
61 lines (54 loc) · 1.82 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
const express = require('express');
const app = express();
const server = require('http').createServer(app);
const io = require('socket.io')(server, {
path: '/socket.io/',
serveClient: false,
pingInterval: 10000,
pingTimeout: 5000,
cookie: false,
cors: {
origin: ["https://launch.playcanvas.com", "http://launch.playcanvas.com", "*"],
methods: ["GET", "POST", "OPTIONS"],
allowedHeaders: ["content-type", "authorization"],
credentials: false
},
allowEIO3: true,
transports: ['polling', 'websocket']
});
// Add CORS middleware for Express
app.use((req, res, next) => {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'GET, POST, OPTIONS');
res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept');
if (req.method === 'OPTIONS') {
return res.sendStatus(200);
}
next();
});
app.get('/', (req, res) => {
res.send('Audio server running');
});
// Track connected clients
const connectedClients = new Set();
io.on('connection', (socket) => {
console.log('Client connected:', socket.id);
connectedClients.add(socket.id);
console.log(`Total connected clients: ${connectedClients.size}`);
socket.on('audioNotification', (packet) => {
console.log('Broadcasting audio from:', socket.id);
socket.broadcast.emit('audioNotification', {
...packet,
sourceId: socket.id
});
});
socket.on('disconnect', () => {
console.log('Client disconnected:', socket.id);
connectedClients.delete(socket.id);
console.log(`Total connected clients: ${connectedClients.size}`);
});
});
const port = process.env.PORT || 3000;
server.listen(port, () => {
console.log(`Server listening on port ${port}`);
});