2022-09-26 20:57:53 -05:00
|
|
|
const PORT = 5588;
|
2022-10-06 12:47:45 -05:00
|
|
|
console.log(`Starting UDP listener on port ${PORT}`);
|
2022-09-26 20:57:53 -05:00
|
|
|
|
2022-10-06 12:47:45 -05:00
|
|
|
const tDec = new TextDecoder();
|
2022-09-27 12:52:11 -05:00
|
|
|
const tEnc = new TextEncoder();
|
2022-09-23 13:33:17 -05:00
|
|
|
|
2022-10-06 12:47:45 -05:00
|
|
|
const reservedData: Record<string, string> = { version: "KeyVal 1.0" };
|
|
|
|
const data = new Map<string, string>();
|
|
|
|
|
2022-10-06 12:54:46 -05:00
|
|
|
const listener = Deno.listenDatagram({
|
|
|
|
hostname: "::",
|
|
|
|
port: PORT,
|
|
|
|
transport: "udp",
|
|
|
|
});
|
2022-10-06 12:47:45 -05:00
|
|
|
|
|
|
|
for await (const [bytes, addr] of listener) {
|
2022-10-06 12:54:46 -05:00
|
|
|
const message = tDec.decode(bytes);
|
2022-10-06 12:47:45 -05:00
|
|
|
if (message.includes("=")) {
|
2022-10-06 12:54:46 -05:00
|
|
|
const [key, ...val] = message.split("=");
|
|
|
|
console.debug(`Setting '${key}' to '${val}'`);
|
|
|
|
if (!(key in reservedData)) data.set(key, val.join("="));
|
2022-10-06 12:47:45 -05:00
|
|
|
} else {
|
|
|
|
const value = reservedData[message] || data.get(message) || "";
|
|
|
|
const bytes = tEnc.encode(`${message}=${value}`);
|
2022-10-06 12:54:46 -05:00
|
|
|
console.debug(`Sending '${message}=${value}'`);
|
2022-10-06 12:47:45 -05:00
|
|
|
await listener.send(bytes, addr);
|
2022-09-27 12:52:11 -05:00
|
|
|
}
|
2022-09-23 14:37:43 -05:00
|
|
|
}
|