Problem 5
This commit is contained in:
parent
d59883d5e1
commit
96c4f9d800
1 changed files with 10 additions and 7 deletions
17
main.ts
17
main.ts
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue