2021-01-10 21:59:07 -05:00
|
|
|
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
|
2020-02-22 18:51:04 -05:00
|
|
|
import { listenAndServe } from "../../http/server.ts";
|
|
|
|
import {
|
|
|
|
acceptable,
|
2020-09-27 06:22:32 -04:00
|
|
|
acceptWebSocket,
|
2020-03-28 13:03:49 -04:00
|
|
|
isWebSocketCloseEvent,
|
2020-09-27 06:22:32 -04:00
|
|
|
WebSocket,
|
2020-02-22 18:51:04 -05:00
|
|
|
} from "../../ws/mod.ts";
|
2020-04-30 06:47:53 -04:00
|
|
|
import { fromFileUrl } from "../../path/mod.ts";
|
2020-02-22 18:51:04 -05:00
|
|
|
|
|
|
|
const clients = new Map<number, WebSocket>();
|
|
|
|
let clientId = 0;
|
2020-03-20 09:38:34 -04:00
|
|
|
function dispatch(msg: string): void {
|
2020-02-22 18:51:04 -05:00
|
|
|
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}]`);
|
2020-05-04 12:27:06 -04:00
|
|
|
for await (const msg of ws) {
|
2020-02-22 18:51:04 -05:00
|
|
|
console.log(`msg:${id}`, msg);
|
|
|
|
if (typeof msg === "string") {
|
|
|
|
dispatch(`[${id}]: ${msg}`);
|
|
|
|
} else if (isWebSocketCloseEvent(msg)) {
|
|
|
|
clients.delete(id);
|
|
|
|
dispatch(`Closed: [${id}]`);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-28 13:03:49 -04:00
|
|
|
listenAndServe({ port: 8080 }, async (req) => {
|
2020-02-22 18:51:04 -05:00
|
|
|
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,
|
2020-04-10 09:51:17 -04:00
|
|
|
fetch(u.href).then(async (resp) => {
|
|
|
|
const body = new Uint8Array(await resp.arrayBuffer());
|
2020-02-24 08:10:00 -05:00
|
|
|
return req.respond({
|
|
|
|
status: resp.status,
|
|
|
|
headers: new Headers({
|
2020-03-28 13:03:49 -04:00
|
|
|
"content-type": "text/html",
|
2020-02-24 08:10:00 -05:00
|
|
|
}),
|
2020-04-10 09:51:17 -04:00
|
|
|
body,
|
2020-02-24 08:10:00 -05:00
|
|
|
});
|
2020-02-22 18:51:04 -05:00
|
|
|
});
|
|
|
|
} else {
|
|
|
|
// server launched by deno run ./server.ts
|
2020-04-30 06:47:53 -04:00
|
|
|
const file = await Deno.open(fromFileUrl(u));
|
2020-02-22 18:51:04 -05:00
|
|
|
req.respond({
|
|
|
|
status: 200,
|
|
|
|
headers: new Headers({
|
2020-03-28 13:03:49 -04:00
|
|
|
"content-type": "text/html",
|
2020-02-22 18:51:04 -05:00
|
|
|
}),
|
2020-03-28 13:03:49 -04:00
|
|
|
body: file,
|
2020-02-22 18:51:04 -05:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2020-02-24 08:10:00 -05:00
|
|
|
if (req.method === "GET" && req.url === "/favicon.ico") {
|
|
|
|
req.respond({
|
|
|
|
status: 302,
|
|
|
|
headers: new Headers({
|
2020-03-28 13:03:49 -04:00
|
|
|
location: "https://deno.land/favicon.ico",
|
|
|
|
}),
|
2020-02-24 08:10:00 -05:00
|
|
|
});
|
|
|
|
}
|
2020-02-22 18:51:04 -05:00
|
|
|
if (req.method === "GET" && req.url === "/ws") {
|
|
|
|
if (acceptable(req)) {
|
|
|
|
acceptWebSocket({
|
|
|
|
conn: req.conn,
|
|
|
|
bufReader: req.r,
|
|
|
|
bufWriter: req.w,
|
2020-03-28 13:03:49 -04:00
|
|
|
headers: req.headers,
|
2020-02-22 18:51:04 -05:00
|
|
|
}).then(wsHandler);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2020-03-24 12:24:58 -04:00
|
|
|
console.log("chat server starting on :8080....");
|