1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-26 09:10:40 -05:00
denoland-deno/std/examples/chat/server.ts
Ryan Dahl 02bc58d832
BREAKING: Make fetch API more web compatible (#4687)
- Removes the __fetch namespace from `deno types`
- Response.redirect should be a static.
- Response.body should not be AsyncIterable.
- Disables the deno_proxy benchmark
- Makes std/examples/curl.ts buffer the body before printing to stdout
2020-04-10 09:51:17 -04:00

79 lines
2 KiB
TypeScript

import { listenAndServe } from "../../http/server.ts";
import {
acceptWebSocket,
acceptable,
WebSocket,
isWebSocketCloseEvent,
} from "../../ws/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.receive()) {
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(u.pathname);
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....");