server ts added
This commit is contained in:
43
src/server.ts
Normal file
43
src/server.ts
Normal file
@@ -0,0 +1,43 @@
|
||||
import { readFileSync } from "fs";
|
||||
import { handleMessage, handleClose } from "./ws-handler";
|
||||
|
||||
const HTML = readFileSync("./src/public/index.html", "utf-8");
|
||||
|
||||
const server = Bun.serve({
|
||||
port: 3000,
|
||||
fetch(req, server) {
|
||||
const url = new URL(req.url);
|
||||
|
||||
if (url.pathname === "/ws") {
|
||||
const ok = server.upgrade(req);
|
||||
if (!ok) return new Response("WebSocket upgrade failed", { status: 400 });
|
||||
return undefined as any;
|
||||
}
|
||||
|
||||
return new Response(HTML, {
|
||||
headers: { "Content-Type": "text/html; charset=utf-8" },
|
||||
});
|
||||
},
|
||||
websocket: {
|
||||
perMessageDeflate: true,
|
||||
maxPayloadLength: 64 * 1024,
|
||||
message(ws, msg) {
|
||||
if (typeof msg !== "string") return;
|
||||
handleMessage(ws, msg);
|
||||
},
|
||||
close(ws) {
|
||||
handleClose(ws);
|
||||
},
|
||||
open(_ws) {},
|
||||
},
|
||||
});
|
||||
|
||||
console.log(`\x1b[32m ██████╗ ██╗ ██╗███████╗███████╗███████╗██████╗ \x1b[0m`);
|
||||
console.log(`\x1b[32m ██╔══██╗██║ ██║╚══███╔╝╚══███╔╝██╔════╝██╔══██╗\x1b[0m`);
|
||||
console.log(`\x1b[32m ██████╔╝██║ ██║ ███╔╝ ███╔╝ █████╗ ██████╔╝\x1b[0m`);
|
||||
console.log(`\x1b[32m ██╔══██╗██║ ██║ ███╔╝ ███╔╝ ██╔══╝ ██╔══██╗\x1b[0m`);
|
||||
console.log(`\x1b[32m ██████╔╝╚██████╔╝███████╗███████╗███████╗██║ ██║\x1b[0m`);
|
||||
console.log(`\x1b[32m ╚═════╝ ╚═════╝ ╚══════╝╚══════╝╚══════╝╚═╝ ╚═╝\x1b[0m`);
|
||||
console.log(`\x1b[2m ─────────────────────────────────────────────────\x1b[0m`);
|
||||
console.log(`\x1b[32m ONLINE\x1b[0m → \x1b[36mhttp://localhost:${server.port}\x1b[0m`);
|
||||
console.log(`\x1b[2m ─────────────────────────────────────────────────\x1b[0m\n`);
|
||||
Reference in New Issue
Block a user