-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.ts
99 lines (94 loc) · 2.58 KB
/
main.ts
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
// ws_server.ts
import { Application, Router, send } from "https://deno.land/x/oak/mod.ts";
const app = new Application({ logErrors: false });
const router = new Router();
router.get("/wss", (ctx) => {
console.log("wss");
if (!ctx.isUpgradable) {
ctx.throw(501);
}
const ws = ctx.upgrade();
// ws.id =
ws.onopen = () => {
clients.push(ws);
console.log("Connected to client", clients.length);
};
ws.onmessage = ({ data }) => {
const parsed = JSON.parse(data);
console.log("Got message from client: ", JSON.parse(data));
switch (parsed.type) {
case "offer":
case "answer":
case "ice-candidate":
// Forward the SDP offer/answer to all other clients
clients.forEach((client) => {
if (client !== ws) {
console.log("sending", parsed.type);
client.send(JSON.stringify(parsed));
}
});
break;
}
};
ws.onclose = () => {
console.log("Disconncted from client");
clients.splice(clients.indexOf(ws), 1);
};
});
app.use(router.routes());
app.use(async (context, next) => {
console.log(context.request.url.pathname);
if (context.request.url.pathname == "/index.html") {
await send(context, context.request.url.pathname, {
root: `${Deno.cwd()}/static`,
index: "index.html",
});
} else {
next();
}
});
app.use(router.allowedMethods());
app.listen({ port: 8000 });
// Array to keep track of connected websockets
const clients: WebSocket[] = [];
async function handleWebSocket(ws: WebSocket) {
try {
clients.push(ws);
console.log("Client connected");
for await (const event of ws) {
if (typeof event === "string") {
handleSignalingData(JSON.parse(event), ws);
} else if (isWebSocketCloseEvent(event)) {
// Handle close events
console.log("Client disconnected");
clients.splice(clients.indexOf(ws), 1);
}
}
} catch (err) {
console.error(`Failed to receive frame: ${err}`);
if (!ws.CLOSED) {
await ws.close(1000).catch(console.error);
}
}
}
function handleSignalingData(data, sender) {
switch (data.type) {
case "offer":
case "answer":
// Forward the SDP offer/answer to all other clients
clients.forEach((client) => {
if (client !== sender) {
client.send(JSON.stringify(data));
}
});
break;
case "ice-candidate":
// Forward ICE candidate to all other clients
clients.forEach((client) => {
if (client !== sender) {
client.send(JSON.stringify(data));
}
});
break;
}
}