56 lines
1.3 KiB
TypeScript
56 lines
1.3 KiB
TypeScript
import type {
|
|
PartySocketEvent,
|
|
PartyState,
|
|
} from "../api/src/party-types";
|
|
|
|
let last_ep_port: null|number = null
|
|
let last_ep_addr: null|string = null
|
|
|
|
const socket = await Bun.udpSocket({
|
|
port: 7070,
|
|
socket: {
|
|
data(socket, buf, port, addr) {
|
|
// console.log(`message from ${addr}:${port}:`);
|
|
last_ep_addr = addr
|
|
last_ep_port = port
|
|
const str = buf.toString();
|
|
console.log(str);
|
|
const opts = str.split(" ").map(t => t.trim().split("="))
|
|
const parsedOpts = Object.fromEntries(opts)
|
|
const { in1, in2, in3, in4, angle } = parsedOpts
|
|
},
|
|
},
|
|
});
|
|
|
|
|
|
let ws: WebSocket | null = new WebSocket("ws://localhost:4000/api/dev-socket/ws");
|
|
|
|
ws.onerror = e => {
|
|
console.error(e)
|
|
}
|
|
|
|
ws.onopen = () => {
|
|
console.log("WebSocket open")
|
|
}
|
|
|
|
ws.onmessage = e => {
|
|
const data = JSON.parse(e.data) as PartySocketEvent;
|
|
console.log(data)
|
|
switch (data.type) {
|
|
case "party_status":
|
|
const { party } = data;
|
|
if (!party) return;
|
|
const partyData = party.data;
|
|
if (!partyData) return;
|
|
const { currentQuestion } = partyData
|
|
console.log(currentQuestion)
|
|
let text = currentQuestion?.text
|
|
if (text && last_ep_port !== null && last_ep_addr !== null) {
|
|
socket.send(text, last_ep_port, last_ep_addr)
|
|
// ws?.send(text)
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
|
|
console.log("Started on :7070")
|