Problem 5

This commit is contained in:
Daniel Flanagan 2022-10-06 12:54:46 -05:00
parent d59883d5e1
commit 96c4f9d800
Signed by: lytedev
GPG key ID: 5B2020A0F9921EF4

17
main.ts
View file

@ -7,19 +7,22 @@ const tEnc = new TextEncoder();
const reservedData: Record<string, string> = { version: "KeyVal 1.0" };
const data = new Map<string, string>();
const listener = Deno.listenDatagram({ port: PORT, transport: "udp" });
const listener = Deno.listenDatagram({
hostname: "::",
port: PORT,
transport: "udp",
});
for await (const [bytes, addr] of listener) {
const message = tDec.decode(bytes).trim();
console.debug("Datagram Received from:", addr, message);
const message = tDec.decode(bytes);
if (message.includes("=")) {
const [key, val] = message.split("=", 1);
console.debug(`Setting ${key} to ${val}`);
if (!(key in reservedData)) data.set(key, val);
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}`);
console.debug(`Sending '${message}=${value}'`);
await listener.send(bytes, addr);
}
}