From 151ce0266eb4de2c8fc600c81c192a5f791b6169 Mon Sep 17 00:00:00 2001 From: Ryan Dahl Date: Wed, 9 Oct 2019 17:10:09 -0400 Subject: Move everything into std subdir --- std/ws/example_client.ts | 58 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 std/ws/example_client.ts (limited to 'std/ws/example_client.ts') diff --git a/std/ws/example_client.ts b/std/ws/example_client.ts new file mode 100644 index 000000000..e3f2f355e --- /dev/null +++ b/std/ws/example_client.ts @@ -0,0 +1,58 @@ +import { + connectWebSocket, + isWebSocketCloseEvent, + isWebSocketPingEvent, + isWebSocketPongEvent +} from "../ws/mod.ts"; +import { encode } from "../strings/mod.ts"; +import { BufReader } from "../io/bufio.ts"; +import { TextProtoReader } from "../textproto/mod.ts"; +import { blue, green, red, yellow } from "../fmt/colors.ts"; + +const endpoint = Deno.args[1] || "ws://127.0.0.1:8080"; +/** simple websocket cli */ +async function main(): Promise { + const sock = await connectWebSocket(endpoint); + console.log(green("ws connected! (type 'close' to quit)")); + (async function(): Promise { + for await (const msg of sock.receive()) { + if (typeof msg === "string") { + console.log(yellow("< " + msg)); + } else if (isWebSocketPingEvent(msg)) { + console.log(blue("< ping")); + } else if (isWebSocketPongEvent(msg)) { + console.log(blue("< pong")); + } else if (isWebSocketCloseEvent(msg)) { + console.log(red(`closed: code=${msg.code}, reason=${msg.reason}`)); + } + } + })(); + const tpr = new TextProtoReader(new BufReader(Deno.stdin)); + while (true) { + await Deno.stdout.write(encode("> ")); + const [line, err] = await tpr.readLine(); + if (err) { + console.error(red(`failed to read line from stdin: ${err}`)); + break; + } + if (line === "close") { + break; + } else if (line === "ping") { + await sock.ping(); + } else { + await sock.send(line); + } + // FIXME: Without this, + // sock.receive() won't resolved though it is readable... + await new Promise((resolve): void => { + setTimeout(resolve, 0); + }); + } + await sock.close(1000); + // FIXME: conn.close() won't shutdown process... + Deno.exit(0); +} + +if (import.meta.main) { + main(); +} -- cgit v1.2.3 From 93f7f00c956c14620ef031626f124b57397ca867 Mon Sep 17 00:00:00 2001 From: Ryan Dahl Date: Wed, 9 Oct 2019 17:22:22 -0400 Subject: Run deno_std tests in github actions --- std/ws/example_client.ts | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) (limited to 'std/ws/example_client.ts') diff --git a/std/ws/example_client.ts b/std/ws/example_client.ts index e3f2f355e..a6649570e 100644 --- a/std/ws/example_client.ts +++ b/std/ws/example_client.ts @@ -44,9 +44,11 @@ async function main(): Promise { } // FIXME: Without this, // sock.receive() won't resolved though it is readable... - await new Promise((resolve): void => { - setTimeout(resolve, 0); - }); + await new Promise( + (resolve): void => { + setTimeout(resolve, 0); + } + ); } await sock.close(1000); // FIXME: conn.close() won't shutdown process... -- cgit v1.2.3