-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsimple_client.ts
More file actions
37 lines (32 loc) · 915 Bytes
/
Copy pathsimple_client.ts
File metadata and controls
37 lines (32 loc) · 915 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import { Client, Packet, Event } from "../mods.ts";
async function echo(listener: Deno.Listener) {
for await (const conn of listener) {
Deno.copy(conn, conn).finally(() => conn.close());
}
}
async function simpleClient() {
const listener = Deno.listen({ port: 8080 });
console.log("listening on 0.0.0.0:8080");
echo(listener);
const client = new Client({ hostname: "127.0.0.1", port: 8080 });
client.on(Event.error, (e) => {
console.error(e);
});
client.on(Event.connect, (client: Client) => {
console.log("Connect", client.conn?.remoteAddr);
client.write("Hello World");
});
client.on(Event.receive, (client: Client, data: Packet) => {
console.log(data.toString());
client.close();
});
client.on(Event.close, (client: Client) => {
console.log("Close");
listener.close();
});
client.connect();
}
async function main() {
simpleClient();
}
main();