-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
73 lines (60 loc) · 2.04 KB
/
server.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
require('dotenv').config();
const express = require('express');
const http = require('http');
const { Server } = require('socket.io');
const cors = require('cors');
const path = require('path');
const stripeRoutes = require('./src/routes/stripeRoutes');
const app = express();
app.use(cors());
app.use(express.json());
// API routes
app.use('/api', stripeRoutes);
// Serve static files from the React build directory
app.use(express.static(path.join(__dirname, 'build')));
const server = http.createServer(app);
const io = new Server(server, {
cors: { origin: "*" },
maxHttpBufferSize: 1e8
});
// Room management
const rooms = new Map();
io.on('connection', (socket) => {
console.log('Client connected:', socket.id);
// Room handling
socket.on('create-room', (roomId) => {
console.log('Creating room:', roomId);
rooms.set(roomId, { initiator: socket.id });
socket.join(roomId);
});
socket.on('join-room', (roomId) => {
console.log('Joining room:', roomId);
socket.join(roomId);
});
// Audio streaming
socket.on('audio-stream', (data) => {
const { roomId, audio, language } = data;
socket.to(roomId).emit('audio-stream', { audio, language });
});
// Translation results
socket.on('translation-result', (data) => {
const { roomId, originalText, translatedText, targetLanguage } = data;
socket.to(roomId).emit('translation-result', { originalText, translatedText, targetLanguage });
});
// Connection management
socket.on('disconnect', () => {
console.log('Client disconnected:', socket.id);
for (const [roomId, room] of rooms.entries()) {
if (room.initiator === socket.id) {
rooms.delete(roomId);
io.to(roomId).emit('room-closed');
}
}
});
});
// Serve React app for all other routes
app.get('*', (req, res) => {
res.sendFile(path.join(__dirname, 'build', 'index.html'));
});
const PORT = process.env.PORT || 3001;
server.listen(PORT, () => console.log(`Server running on port ${PORT}`));