diff options
Diffstat (limited to 'std/examples/chat')
-rw-r--r-- | std/examples/chat/index.html | 81 | ||||
-rw-r--r-- | std/examples/chat/server.ts | 81 | ||||
-rw-r--r-- | std/examples/chat/server_test.ts | 81 |
3 files changed, 0 insertions, 243 deletions
diff --git a/std/examples/chat/index.html b/std/examples/chat/index.html deleted file mode 100644 index 2daf288b3..000000000 --- a/std/examples/chat/index.html +++ /dev/null @@ -1,81 +0,0 @@ -<html lang="en"> - <head> - <meta charset="UTF-8" /> - <title>ws chat example</title> - </head> - <body> - <div> - <input type="text" id="input" /> - <button id="sendButton" disabled>send</button> - <button id="connectButton" disabled>connect</button> - <button id="closeButton" disabled>close</button> - </div> - <div id="status"></div> - <ul id="timeline"></ul> - <script> - let ws; - function messageDom(msg) { - const div = document.createElement("li"); - div.className = "message"; - div.innerText = msg; - return div; - } - const timeline = document.getElementById("timeline"); - const sendButton = document.getElementById("sendButton"); - sendButton.onclick = send; - const closeButton =document.getElementById("closeButton"); - closeButton.onclick=close; - const connectButton = document.getElementById("connectButton"); - connectButton.onclick=connect; - const status = document.getElementById("status"); - const input = document.getElementById("input"); - input.addEventListener("keydown", keyDownEvent); - function send() { - const msg = input.value; - ws.send(msg); - applyState({inputValue: ""}); - } - function keyDownEvent(e) { - if (e.keyCode === 13) return send(); - } - function connect() { - if (ws) ws.close(); - ws = new WebSocket(`ws://${location.host}/ws`); - ws.addEventListener("open", () => { - console.log("open", ws); - applyState({connected: true}); - }); - ws.addEventListener("message", ({data}) => { - timeline.appendChild(messageDom(data)); - }); - ws.addEventListener("close", () => { - applyState({connect: false}); - }); - } - function close() { - ws.close(); - applyState({connected: false}); - } - function applyState({connected, status, inputValue}) { - if (inputValue != null) { - input.value = inputValue; - } - if(status != null) { - status.innerText = status; - } - if (connected != null) { - if (connected) { - sendButton.disabled = false; - connectButton.disabled = true; - closeButton.disabled= false; - } else { - sendButton.disabled= true; - connectButton.disabled=false; - closeButton.disabled=true; - } - } - } - connect(); - </script> - </body> -</html> 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...."); diff --git a/std/examples/chat/server_test.ts b/std/examples/chat/server_test.ts deleted file mode 100644 index 80a66cb2f..000000000 --- a/std/examples/chat/server_test.ts +++ /dev/null @@ -1,81 +0,0 @@ -// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license. -import { assert, assertEquals } from "../../testing/asserts.ts"; -import { TextProtoReader } from "../../textproto/mod.ts"; -import { BufReader } from "../../io/bufio.ts"; -import { delay } from "../../async/delay.ts"; -import { dirname, fromFileUrl, resolve } from "../../path/mod.ts"; - -const moduleDir = resolve(dirname(fromFileUrl(import.meta.url))); - -async function startServer(): Promise< - Deno.Process<Deno.RunOptions & { stdout: "piped" }> -> { - const server = Deno.run({ - cmd: [ - Deno.execPath(), - "run", - "--quiet", - "--allow-net", - "--allow-read", - "server.ts", - ], - cwd: moduleDir, - stdout: "piped", - }); - try { - assert(server.stdout != null); - const r = new TextProtoReader(new BufReader(server.stdout)); - const s = await r.readLine(); - assert(s !== null && s.includes("chat server starting")); - } catch (err) { - server.stdout.close(); - server.close(); - } - - return server; -} - -Deno.test({ - name: "[examples/chat] GET / should serve html", - async fn() { - const server = await startServer(); - try { - const resp = await fetch("http://127.0.0.1:8080/"); - assertEquals(resp.status, 200); - assertEquals(resp.headers.get("content-type"), "text/html"); - const html = await resp.text(); - assert(html.includes("ws chat example"), "body is ok"); - } finally { - server.close(); - server.stdout.close(); - } - await delay(10); - }, -}); - -Deno.test({ - name: "[examples/chat] GET /ws should upgrade conn to ws", - async fn() { - const server = await startServer(); - let ws: WebSocket; - try { - ws = new WebSocket("ws://127.0.0.1:8080/ws"); - await new Promise<void>((resolve) => { - ws.onmessage = ((message) => { - assertEquals(message.data, "Connected: [1]"); - ws.onmessage = ((message) => { - assertEquals(message.data, "[1]: Hello"); - ws.close(); - resolve(); - }); - ws.send("Hello"); - }); - }); - } catch (err) { - console.log(err); - } finally { - server.close(); - server.stdout.close(); - } - }, -}); |