diff options
author | Casper Beyer <caspervonb@pm.me> | 2021-02-02 19:05:46 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-02-02 12:05:46 +0100 |
commit | 6abf126c2a7a451cded8c6b5e6ddf1b69c84055d (patch) | |
tree | fd94c013a19fcb38954844085821ec1601c20e18 /std/examples/chat/server.ts | |
parent | a2b5d44f1aa9d64f448a2a3cc2001272e2f60b98 (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/examples/chat/server.ts')
-rw-r--r-- | std/examples/chat/server.ts | 81 |
1 files changed, 0 insertions, 81 deletions
diff --git a/std/examples/chat/server.ts b/std/examples/chat/server.ts deleted file mode 100644 index 39b5ee124..000000000 --- a/std/examples/chat/server.ts +++ /dev/null @@ -1,81 +0,0 @@ -// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license. -import { listenAndServe } from "../../http/server.ts"; -import { - acceptable, - acceptWebSocket, - isWebSocketCloseEvent, - WebSocket, -} from "../../ws/mod.ts"; -import { fromFileUrl } from "../../path/mod.ts"; - -const clients = new Map<number, WebSocket>(); -let clientId = 0; -function dispatch(msg: string): void { - for (const client of clients.values()) { - client.send(msg); - } -} -async function wsHandler(ws: WebSocket): Promise<void> { - const id = ++clientId; - clients.set(id, ws); - dispatch(`Connected: [${id}]`); - for await (const msg of ws) { - console.log(`msg:${id}`, msg); - if (typeof msg === "string") { - dispatch(`[${id}]: ${msg}`); - } else if (isWebSocketCloseEvent(msg)) { - clients.delete(id); - dispatch(`Closed: [${id}]`); - break; - } - } -} - -listenAndServe({ port: 8080 }, async (req) => { - if (req.method === "GET" && req.url === "/") { - //Serve with hack - const u = new URL("./index.html", import.meta.url); - if (u.protocol.startsWith("http")) { - // server launched by deno run http(s)://.../server.ts, - fetch(u.href).then(async (resp) => { - const body = new Uint8Array(await resp.arrayBuffer()); - return req.respond({ - status: resp.status, - headers: new Headers({ - "content-type": "text/html", - }), - body, - }); - }); - } else { - // server launched by deno run ./server.ts - const file = await Deno.open(fromFileUrl(u)); - req.respond({ - status: 200, - headers: new Headers({ - "content-type": "text/html", - }), - body: file, - }); - } - } - if (req.method === "GET" && req.url === "/favicon.ico") { - req.respond({ - status: 302, - headers: new Headers({ - location: "https://deno.land/favicon.ico", - }), - }); - } - if (req.method === "GET" && req.url === "/ws") { - if (acceptable(req)) { - acceptWebSocket({ - conn: req.conn, - bufReader: req.r, - bufWriter: req.w, - headers: req.headers, - }).then(wsHandler); - } - } -}); -console.log("chat server starting on :8080...."); |