-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
152 lines (112 loc) · 3.59 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
const express = require('express');
const http = require('http');
const socketIo = require('socket.io');
const cors = require('cors');
const { v4: uuidv4 } = require('uuid');
const app = express();
const server = http.createServer(app);
const allowedOrigins = [
"http://localhost:3000",
"https://youtube-syn.vercel.app/" // You'll update this with your actual Vercel URL
];
app.use(express.json());
// app.use(cors({
// origin: function(origin, callback) {
// if (!origin || allowedOrigins.indexOf(origin) !== -1) {
// callback(null, true);
// } else {
// callback(new Error('Not allowed by CORS'));
// }
// },
// methods: ["GET", "POST"],
// credentials: true
// }));
app.use(cors({
origin: "*", // Temporarily allow all origins
methods: ["GET", "POST"],
credentials: true
}));
const io = socketIo(server, {
cors: {
origin: "*", // Allow all origins temporarily
methods: ["GET", "POST"],
credentials: true
}
});
const rooms = new Map();
const generateRoomId = () => {
return Math.random().toString(36).substr(2, 4).toUpperCase(); // Generate 8-character room ID
};
app.post('/create-room', (req, res) => {
const roomId = generateRoomId();
// console.log("thank you for creating a room and your roomId is here: : ", roomId);
const { url } = req.body;
// console.log("and your youtube link is: ", url);
rooms.set(roomId, {
url,
users: new Set(),
});
res.json({ roomId, url });
});
app.post('/join-room', (req, res) => {
const { roomId } = req.body;
// console.log("joining room id ",roomId)
if (!rooms.has(roomId)) {
return res.status(404).json({ error: 'Room not found' });
}
const room = rooms.get(roomId);
// console.log("thank you for joing a roomiD : ", rooms.get(roomId));
// console.log("with url: : ", room.url);
res.json({ roomId, url: room.url });
});
io.on('connection', (socket) => {
let currentRoom = null;
socket.on('joinRoom', ({ roomId, url }) => {
if (!rooms.has(roomId)) return;
// console.log("hi you cliend id is: ",socket.id.slice(0, 4));
// console.log("and url is: ", url);
currentRoom = roomId;
socket.join(roomId);
const room = rooms.get(roomId);
room.users.add(socket.id);
io.to(roomId).emit('roomUsers', Array.from(room.users));
});
socket.on('syncVideo', ({ roomId, time, playbackSpeed }) => {
if (!rooms.has(roomId)) return;
console.log("this SynViedo ok: ", roomId,time, playbackSpeed);
const room = rooms.get(roomId);
room.currentTime = time;
room.playbackSpeed = playbackSpeed;
socket.to(roomId).emit('syncVideo', { time, playbackSpeed });
});
socket.on('play', ({ roomId, time, playbackSpeed }) => {
socket.to(roomId).emit('play', { time, playbackSpeed });
});
socket.on('pause', ({ roomId, time }) => {
socket.to(roomId).emit('pause', { time });
});
socket.on('seek', ({ roomId, time }) => {
socket.to(roomId).emit('seek', { time });
});
socket.on('mute', ({ roomId }) => {
socket.to(roomId).emit('mute');
});
socket.on('unmute', ({ roomId }) => {
socket.to(roomId).emit('unmute');
});
socket.on('disconnect', () => {
if (currentRoom && rooms.has(currentRoom)) {
const room = rooms.get(currentRoom);
room.users.delete(socket.id);
if (room.users.size === 0) {
rooms.delete(currentRoom);
} else {
io.to(currentRoom).emit('roomUsers', Array.from(room.users));
}
}
});
});
const PORT = process.env.PORT || 4000;
server.listen(PORT, '0.0.0.0', () => {
console.log(`Server running on port ${PORT}`);
});