-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.ts
More file actions
114 lines (97 loc) · 3.34 KB
/
Copy pathserver.ts
File metadata and controls
114 lines (97 loc) · 3.34 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
import express from "express";
import { createServer as createViteServer } from "vite";
import { WebSocketServer, WebSocket } from "ws";
import http from "http";
import { v4 as uuidv4 } from "uuid";
interface Client {
id: string;
ws: WebSocket;
name: string;
deviceType: string;
}
const clients = new Map<string, Client>();
async function startServer() {
const app = express();
const PORT = 3000;
const server = http.createServer(app);
const wss = new WebSocketServer({ server, path: '/ws' });
// Broadcast updated client list to everyone
const broadcastClients = () => {
const clientList = Array.from(clients.values()).map(c => ({
id: c.id,
name: c.name,
deviceType: c.deviceType
}));
const message = JSON.stringify({ type: 'peers', peers: clientList });
for (const client of clients.values()) {
if (client.ws.readyState === WebSocket.OPEN) {
client.ws.send(message);
}
}
};
wss.on('connection', (ws) => {
const clientId = uuidv4();
let clientName = `Device-${clientId.substring(0, 4)}`;
let clientDeviceType = 'desktop';
clients.set(clientId, { id: clientId, ws, name: clientName, deviceType: clientDeviceType });
// Send the client its own ID
ws.send(JSON.stringify({ type: 'welcome', id: clientId }));
// Broadcast new list
broadcastClients();
ws.on('message', (message) => {
try {
const data = JSON.parse(message.toString());
if (data.type === 'update-info') {
const client = clients.get(clientId);
if (client) {
client.name = data.name || client.name;
client.deviceType = data.deviceType || client.deviceType;
broadcastClients();
}
} else if (data.type === 'offer' || data.type === 'answer' || data.type === 'ice-candidate') {
// Forward signaling messages to the target peer
const targetClient = clients.get(data.target);
if (targetClient && targetClient.ws.readyState === WebSocket.OPEN) {
targetClient.ws.send(JSON.stringify({
...data,
from: clientId
}));
}
} else if (data.type === 'transfer-request' || data.type === 'transfer-response' || data.type === 'transfer-progress' || data.type === 'transfer-complete' || data.type === 'transfer-cancel') {
// Forward transfer control messages
const targetClient = clients.get(data.target);
if (targetClient && targetClient.ws.readyState === WebSocket.OPEN) {
targetClient.ws.send(JSON.stringify({
...data,
from: clientId
}));
}
}
} catch (e) {
console.error('Failed to parse message', e);
}
});
ws.on('close', () => {
clients.delete(clientId);
broadcastClients();
});
});
// API routes
app.get("/api/health", (req, res) => {
res.json({ status: "ok" });
});
// Vite middleware for development
if (process.env.NODE_ENV !== "production") {
const vite = await createViteServer({
server: { middlewareMode: true },
appType: "spa",
});
app.use(vite.middlewares);
} else {
app.use(express.static('dist'));
}
server.listen(PORT, "0.0.0.0", () => {
console.log(`Server running on http://localhost:${PORT}`);
});
}
startServer();