deno.land / std@0.166.0 / examples / chat / server.ts

نووسراو ببینە
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.import { fromFileUrl } from "../../path/mod.ts";import { readableStreamFromReader } from "../../streams/conversion.ts";
const clients = new Map<number, WebSocket>();let clientId = 0;function dispatch(msg: string) { for (const client of clients.values()) { client.send(msg); }}
function wsHandler(ws: WebSocket) { const id = ++clientId; clients.set(id, ws); ws.onopen = () => { dispatch(`Connected: [${id}]`); }; ws.onmessage = (e) => { console.log(`msg:${id}`, e.data); dispatch(`[${id}]: ${e.data}`); }; ws.onclose = () => { clients.delete(id); dispatch(`Closed: [${id}]`); };}
async function requestHandler(req: Deno.RequestEvent) { const pathname = new URL(req.request.url).pathname; if (req.request.method === "GET" && pathname === "/") { //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()); req.respondWith( new Response(body, { status: resp.status, headers: { "content-type": "text/html", }, }), ); }); } else { // server launched by deno run ./server.ts const file = await Deno.open(fromFileUrl(u)); req.respondWith( new Response(readableStreamFromReader(file), { status: 200, headers: { "content-type": "text/html", }, }), ); } } else if ( req.request.method === "GET" && pathname === "/favicon.ico" ) { req.respondWith(Response.redirect("https://deno.land/favicon.ico", 302)); } else if (req.request.method === "GET" && pathname === "/ws") { const { socket, response } = Deno.upgradeWebSocket(req.request); wsHandler(socket); req.respondWith(response); }}
const server = Deno.listen({ port: 8080 });console.log("chat server starting on :8080....");
for await (const conn of server) { (async () => { const httpConn = Deno.serveHttp(conn); for await (const requestEvent of httpConn) { requestHandler(requestEvent); } })();}
std

Version Info

Tagged at
a year ago