Fix 0 and 1

This commit is contained in:
Daniel Flanagan 2022-10-11 13:39:54 -05:00
parent 68178cee60
commit 95cee18537
Signed by untrusted user: lytedev-divvy
GPG key ID: 6D69CEEE4ABBCD82
2 changed files with 10 additions and 2 deletions

4
0.ts
View file

@ -5,6 +5,10 @@ const PORT = 5588;
console.log(`Listening on port ${PORT}`); console.log(`Listening on port ${PORT}`);
for await (const conn of Deno.listen({ port: PORT })) { for await (const conn of Deno.listen({ port: PORT })) {
echo(conn);
}
async function echo(conn: Deno.Conn) {
await copy(conn, conn); await copy(conn, conn);
conn.close(); conn.close();
} }

8
1.ts
View file

@ -6,6 +6,10 @@ const tEnc = new TextEncoder();
console.log(`Listening on port ${PORT}`); console.log(`Listening on port ${PORT}`);
for await (const conn of Deno.listen({ port: PORT })) { for await (const conn of Deno.listen({ port: PORT })) {
processRequestStream(conn);
}
async function processRequestStream(conn: Deno.Conn) {
console.log("Connection established:", conn.remoteAddr); console.log("Connection established:", conn.remoteAddr);
try { try {
for await (const request of requests(conn)) { for await (const request of requests(conn)) {
@ -25,10 +29,10 @@ for await (const conn of Deno.listen({ port: PORT })) {
prime: isPrime(request.number), prime: isPrime(request.number),
}; };
console.debug("Response:", response); console.debug("Response:", response);
await conn.write(tEnc.encode(JSON.stringify(response) + "\n")); conn.write(tEnc.encode(JSON.stringify(response) + "\n"));
} else { } else {
console.debug("Request malformed"); console.debug("Request malformed");
await conn.write(tEnc.encode("Malformed!\n")); conn.write(tEnc.encode("Malformed!\n"));
} }
} }
} catch (e) { } catch (e) {