Problem 5
This commit is contained in:
parent
d59883d5e1
commit
96c4f9d800
17
main.ts
17
main.ts
|
@ -7,19 +7,22 @@ const tEnc = new TextEncoder();
|
||||||
const reservedData: Record<string, string> = { version: "KeyVal 1.0" };
|
const reservedData: Record<string, string> = { version: "KeyVal 1.0" };
|
||||||
const data = new Map<string, string>();
|
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) {
|
for await (const [bytes, addr] of listener) {
|
||||||
const message = tDec.decode(bytes).trim();
|
const message = tDec.decode(bytes);
|
||||||
console.debug("Datagram Received from:", addr, message);
|
|
||||||
if (message.includes("=")) {
|
if (message.includes("=")) {
|
||||||
const [key, val] = message.split("=", 1);
|
const [key, ...val] = message.split("=");
|
||||||
console.debug(`Setting ${key} to ${val}`);
|
console.debug(`Setting '${key}' to '${val}'`);
|
||||||
if (!(key in reservedData)) data.set(key, val);
|
if (!(key in reservedData)) data.set(key, val.join("="));
|
||||||
} else {
|
} else {
|
||||||
const value = reservedData[message] || data.get(message) || "";
|
const value = reservedData[message] || data.get(message) || "";
|
||||||
const bytes = tEnc.encode(`${message}=${value}`);
|
const bytes = tEnc.encode(`${message}=${value}`);
|
||||||
console.debug(`Sending ${message}=${value}`);
|
console.debug(`Sending '${message}=${value}'`);
|
||||||
await listener.send(bytes, addr);
|
await listener.send(bytes, addr);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue