29 lines
851 B
TypeScript
29 lines
851 B
TypeScript
const PORT = 5588;
|
|
console.log(`Starting UDP listener on port ${PORT}`);
|
|
|
|
const tDec = new TextDecoder();
|
|
const tEnc = new TextEncoder();
|
|
|
|
const reservedData: Record<string, string> = { version: "KeyVal 1.0" };
|
|
const data = new Map<string, string>();
|
|
|
|
const listener = Deno.listenDatagram({
|
|
hostname: "::",
|
|
port: PORT,
|
|
transport: "udp",
|
|
});
|
|
|
|
for await (const [bytes, addr] of listener) {
|
|
const message = tDec.decode(bytes);
|
|
if (message.includes("=")) {
|
|
const [key, ...val] = message.split("=");
|
|
console.debug(`Setting '${key}' to '${val}'`);
|
|
if (!(key in reservedData)) data.set(key, val.join("="));
|
|
} else {
|
|
const value = reservedData[message] || data.get(message) || "";
|
|
const bytes = tEnc.encode(`${message}=${value}`);
|
|
console.debug(`Sending '${message}=${value}'`);
|
|
await listener.send(bytes, addr);
|
|
}
|
|
}
|