-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathconnect.ts
executable file
·54 lines (41 loc) · 1.16 KB
/
connect.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
#!/usr/bin/env deno --unstable-net run --allow-all
const abletonPort = 11000;
const localOscPort = 11001;
const webSocketServerPort = 3000;
let webSocket: WebSocket | undefined;
const udpSocket = Deno.listenDatagram({ port: localOscPort, transport: "udp" });
const headers = new Headers({
"Access-Control-Allow-Origin": "https://live.vroomai.com"
})
Deno.serve({
port: webSocketServerPort,
hostname: "0.0.0.0",
}, (req) => {
if (req.headers.get("upgrade") != "websocket") {
return new Response(null, { status: 501, headers });
}
const { socket: _socket, response } = Deno.upgradeWebSocket(req);
webSocket = _socket;
webSocket.addEventListener("message", (event) => {
const data = event.data;
console.log(data);
if (!(data instanceof ArrayBuffer)) {
throw new Error("Data from WebSocket not ArrayBuffer.");
}
udpSocket.send(new Uint8Array(data), {
transport: "udp",
hostname: "127.0.0.1",
port: abletonPort,
});
});
return response;
});
(async () => {
for await (const [data] of udpSocket) {
if (webSocket === undefined) {
console.warn("Recieved OSC data but no WebSocket connected.");
continue;
}
webSocket.send(data);
}
})();