summaryrefslogtreecommitdiff
path: root/std/ws/example_server.ts
diff options
context:
space:
mode:
authorCasper Beyer <caspervonb@pm.me>2021-02-02 19:05:46 +0800
committerGitHub <noreply@github.com>2021-02-02 12:05:46 +0100
commit6abf126c2a7a451cded8c6b5e6ddf1b69c84055d (patch)
treefd94c013a19fcb38954844085821ec1601c20e18 /std/ws/example_server.ts
parenta2b5d44f1aa9d64f448a2a3cc2001272e2f60b98 (diff)
chore: remove std directory (#9361)
This removes the std folder from the tree. Various parts of the tests are pretty tightly dependent on std (47 direct imports and 75 indirect imports, not counting the cli tests that use them as fixtures) so I've added std as a submodule for now.
Diffstat (limited to 'std/ws/example_server.ts')
-rw-r--r--std/ws/example_server.ts58
1 files changed, 0 insertions, 58 deletions
diff --git a/std/ws/example_server.ts b/std/ws/example_server.ts
deleted file mode 100644
index 0e99bd03a..000000000
--- a/std/ws/example_server.ts
+++ /dev/null
@@ -1,58 +0,0 @@
-// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
-import { serve } from "../http/server.ts";
-import {
- acceptWebSocket,
- isWebSocketCloseEvent,
- isWebSocketPingEvent,
- WebSocket,
-} from "./mod.ts";
-
-async function handleWs(sock: WebSocket): Promise<void> {
- console.log("socket connected!");
- try {
- for await (const ev of sock) {
- if (typeof ev === "string") {
- // text message
- console.log("ws:Text", ev);
- await sock.send(ev);
- } else if (ev instanceof Uint8Array) {
- // binary message
- console.log("ws:Binary", ev);
- } else if (isWebSocketPingEvent(ev)) {
- const [, body] = ev;
- // ping
- console.log("ws:Ping", body);
- } else if (isWebSocketCloseEvent(ev)) {
- // close
- const { code, reason } = ev;
- console.log("ws:Close", code, reason);
- }
- }
- } catch (err) {
- console.error(`failed to receive frame: ${err}`);
-
- if (!sock.isClosed) {
- await sock.close(1000).catch(console.error);
- }
- }
-}
-
-if (import.meta.main) {
- /** websocket echo server */
- const port = Deno.args[0] || "8080";
- console.log(`websocket server is running on :${port}`);
- for await (const req of serve(`:${port}`)) {
- const { conn, r: bufReader, w: bufWriter, headers } = req;
- acceptWebSocket({
- conn,
- bufReader,
- bufWriter,
- headers,
- })
- .then(handleWs)
- .catch(async (e) => {
- console.error(`failed to accept websocket: ${e}`);
- await req.respond({ status: 400 });
- });
- }
-}