-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.js
More file actions
366 lines (314 loc) · 10.2 KB
/
server.js
File metadata and controls
366 lines (314 loc) · 10.2 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
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
import express from 'express';
import { WebSocketServer } from 'ws';
import { createServer } from 'http';
import path from 'path';
import { fileURLToPath } from 'url';
import * as Y from 'yjs';
import { docs, setupWSConnection } from 'y-websocket/bin/utils';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const app = express();
const server = createServer(app);
const wss = new WebSocketServer({ server });
// Serve static files
app.use(express.static('.'));
// Add JSON bodt parser
app.use(express.json());
// Gemini endpoint
app.post('/api/gemini', async (req, res) => {
try {
const { prompt, history = [] } = req.body;
if (!prompt) {
return res.status(400).json({ error: 'Prompt is required' });
}
// Import Gemini
const { GoogleGenerativeAI } = await import("@google/generative-ai");
// Use your own API key
const genAI = new GoogleGenerativeAI("your-API-key");
// adjust the model as you wish
const model = genAI.getGenerativeModel({ model: "gemini-2.5-flash-lite" });
// Start chat with history
const chat = model.startChat({
history: history
});
// Send message
const result = await chat.sendMessage(prompt);
const response = result.response;
const text = response.text();
res.json({ response: text });
} catch (error) {
console.error('Gemini API error:', error);
res.status(500).json({ error: 'Failed to get response from Gemini' });
}
});
// Endpoint to clear Yjs document cache
app.post('/api/clear-yjs-cache/:docName?', (req, res) => {
const docName = req.params.docName;
if (docName) {
const doc = docs.get(docName);
// Clear specific document
if (doc) {
doc.destroy();
docs.delete(docName);
console.log(`Cleared Yjs document: ${docName}`);
res.json({ success: true, message: `Cleared document: ${docName}` });
} else {
res.json({ success: false, message: `Document not found: ${docName}` });
}
} else {
// Clear all documents
const count = docs.size;
docs.clear();
console.log(`Cleared all ${count} Yjs documents`);
res.json({ success: true, message: `Cleared ${count} documents` });
}
});
// GET endpoint for easier browser access
app.get('/api/clear-yjs-cache/:docName?', (req, res) => {
const docName = req.params.docName;
if (docName) {
const doc = docs.get(docName);
if (doc) {
doc.destroy();
docs.delete(docName);
console.log(`Cleared Yjs document: ${docName}`);
res.send(`<h1>Cleared document: ${docName}</h1><p><a href="/">Back to BICI</a></p>`);
} else {
res.send(`<h1>Document not found: ${docName}</h1><p><a href="/">Back to BICI</a></p>`);
}
} else {
const count = docs.size;
docs.forEach((doc) => doc.destroy());
docs.clear();
console.log(`Cleared all ${count} Yjs documents`);
res.send(`<h1>Cleared ${count} documents</h1><p><a href="/">Back to BICI</a></p>`);
}
});
// Store connected clients
const clients = new Map();
// Store rooms: roomId -> { clients: Set<clientId>, createdAt: timestamp }
const rooms = new Map();
// Store client-to-room mapping: clientId -> roomId
const clientRooms = new Map();
// Generate short room code (6 alphanumeric characters)
function generateRoomCode() {
const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
let code = '';
for (let i = 0; i < 6; i++) {
code += chars.charAt(Math.floor(Math.random() * chars.length));
}
// Ensure uniqueness
return rooms.has(code) ? generateRoomCode() : code;
}
// Create a new room
function createRoom(roomId = null) {
const id = roomId || generateRoomCode();
if (!rooms.has(id)) {
rooms.set(id, {
clients: new Set(),
createdAt: Date.now()
});
console.log(`Created room: ${id}`);
}
return id;
}
// Add client to room
function addClientToRoom(clientId, roomId) {
const room = rooms.get(roomId);
if (!room) {
console.error(`Room ${roomId} does not exist`);
return false;
}
// Check room capacity (max 2 for 1-on-1)
if (room.clients.size >= 2) {
console.log(`Room ${roomId} is full (${room.clients.size}/2)`);
return false;
}
room.clients.add(clientId);
clientRooms.set(clientId, roomId);
console.log(`Client ${clientId} joined room ${roomId} (${room.clients.size}/2)`);
return true;
}
// Remove client from room and cleanup if empty
function removeClientFromRoom(clientId) {
const roomId = clientRooms.get(clientId);
if (!roomId) return;
const room = rooms.get(roomId);
if (room) {
room.clients.delete(clientId);
console.log(`Client ${clientId} left room ${roomId} (${room.clients.size}/2)`);
// Auto-delete room if empty
if (room.clients.size === 0) {
rooms.delete(roomId);
console.log(`Deleted empty room: ${roomId}`);
}
}
clientRooms.delete(clientId);
}
// Get clients in same room
function getRoomClients(roomId) {
const room = rooms.get(roomId);
return room ? Array.from(room.clients) : [];
}
wss.on('connection', (ws, req) => {
// Check if this is a y-websocket connection (has docName in URL)
const url = new URL(req.url, `http://${req.headers.host}`);
const docName = url.pathname.slice(1); // Remove leading '/'
if (docName) {
console.log(`Yjs client connected to document: ${docName}`);
setupWSConnection(ws, req, { docName });
return
}
// Regular WebRTC signaling connection
const clientId = Math.random().toString(36).substr(2, 9);
clients.set(clientId, ws);
console.log(`Client connected: ${clientId}. Total clients: ${clients.size}`);
// Parse room ID from URL query parameters
const roomIdFromUrl = url.searchParams.get('room');
let roomId = null;
let roomJoinSuccess = false;
let roomFull = false;
if (roomIdFromUrl) {
// Client wants to join a specific room
if (!rooms.has(roomIdFromUrl)) {
// Room doesn't exist, create it
createRoom(roomIdFromUrl);
}
roomJoinSuccess = addClientToRoom(clientId, roomIdFromUrl);
if (roomJoinSuccess) {
roomId = roomIdFromUrl;
} else {
roomFull = true;
}
} else {
// No room specified, auto-create a new room
roomId = createRoom();
addClientToRoom(clientId, roomId);
roomJoinSuccess = true;
}
// Send the client their ID and room info
ws.send(JSON.stringify({
type: 'welcome',
clientId: clientId,
roomId: roomId,
roomFull: roomFull,
totalClients: clients.size
}));
if (roomJoinSuccess) {
// Broadcast updated client list to clients in the same room
broadcastClientListToRoom(roomId);
}
ws.on('message', (message) => {
try {
const data = JSON.parse(message.toString());
// Handle different message types
switch (data.type) {
case 'offer':
case 'answer':
case 'ice-candidate':
// Forward WebRTC signaling messages to the target peer
const targetClient = clients.get(data.target);
if (targetClient && targetClient.readyState === 1) {
targetClient.send(JSON.stringify({
...data,
from: clientId
}));
}
break;
case 'request-client-list':
// Send current client list to requester
sendClientList(ws, clientId);
break;
case 'state-update':
// Broadcast state updates to clients in the same room only
const senderRoomId = clientRooms.get(clientId);
if (senderRoomId) {
console.log('Broadcasting state update from:', clientId, 'in room:', senderRoomId);
const roomClients = getRoomClients(senderRoomId);
roomClients.forEach((id) => {
if (id !== clientId) {
const client = clients.get(id);
if (client && client.readyState === 1) {
client.send(JSON.stringify({
type: 'state-update',
from: clientId,
state: data.state
}));
}
}
});
}
break;
case 'action':
// Relay action from secondary client to master client
console.log('Relaying action from:', clientId, 'to master:', data.to);
const masterClient = clients.get(data.to);
if (masterClient && masterClient.readyState === 1) {
masterClient.send(JSON.stringify({
type: 'action',
from: clientId,
action: data.action
}));
}
break;
default:
console.log('Unknown message type:', data.type);
}
} catch (error) {
console.error('Error processing message:', error);
}
});
ws.on('close', () => {
const roomId = clientRooms.get(clientId);
clients.delete(clientId);
removeClientFromRoom(clientId);
console.log(`Client disconnected: ${clientId}. Total clients: ${clients.size}`);
// Notify other clients in the room
if (roomId) {
broadcastClientListToRoom(roomId);
}
});
ws.on('error', (error) => {
console.error('WebSocket error:', error);
});
});
// Broadcast client list to all clients in a specific room
function broadcastClientListToRoom(roomId) {
const roomClientIds = getRoomClients(roomId);
const message = JSON.stringify({
type: 'client-list',
clients: roomClientIds
});
roomClientIds.forEach((clientId) => {
const client = clients.get(clientId);
if (client && client.readyState === 1) { // OPEN
client.send(message);
}
});
}
// Legacy function for backwards compatibility (now uses rooms)
function broadcastClientList() {
rooms.forEach((room, roomId) => {
broadcastClientListToRoom(roomId);
});
}
function sendClientList(ws, excludeId) {
const roomId = clientRooms.get(excludeId);
if (!roomId) {
ws.send(JSON.stringify({
type: 'client-list',
clients: []
}));
return;
}
const clientIds = getRoomClients(roomId).filter(id => id !== excludeId);
ws.send(JSON.stringify({
type: 'client-list',
clients: clientIds
}));
}
const PORT = process.env.PORT || 8000;
server.listen(PORT, () => {
console.log(`🚀 BICI server running on http://localhost:${PORT}`);
console.log(`📡 WebRTC signaling server ready for connections`);
});