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>();
|
|
|
|
|
|
|
|
const listener = Deno.listenDatagram({ port: PORT, transport: "udp" });
|
|
|
|
|
|
|
|
for await (const [bytes, addr] of listener) {
|
|
|
|
const message = tDec.decode(bytes).trim();
|
|
|
|
console.debug("Datagram Received from:", addr, message);
|
|
|
|
if (message.includes("=")) {
|
|
|
|
const [key, val] = message.split("=", 1);
|
|
|
|
console.debug(`Setting ${key} to ${val}`);
|
|
|
|
if (!(key in reservedData)) data.set(key, val);
|
|
|
|
} else {
|
|
|
|
const value = reservedData[message] || data.get(message) || "";
|
|
|
|
const bytes = tEnc.encode(`${message}=${value}`);
|
|
|
|
console.debug(`Sending ${message}=${value}`);
|
|
|
|
await listener.send(bytes, addr);
|
2022-09-27 12:52:11 -05:00
|
|
|
}
|
2022-09-23 14:37:43 -05:00
|
|
|
}
|