checkpoint 2: websocket echo + test page
This commit is contained in:
1 parent
2c783e07ca
commit
8cb4c3556b
2 files changed
+49
-9
No files matched your search
@@ -18,6 +18,12 @@ const pages: Record<string, string> = {
|
||||
"/trading": "trading.html",
|
||||
"/ws-test": "ws-test.html",
|
||||
};
|
||||
|
||||
async function serveFile(path: string): Promise<Response> {
|
||||
const file = Bun.file(path);
|
||||
if (await file.exists()) return new Response(file);
|
||||
return new Response("Not Found", { status: 404 });
|
||||
}
|
||||
const server = Bun.serve<WebSocketData>({
|
||||
port: 3000,
|
||||
async fetch(req, server) {
|
||||
@@ -32,13 +38,7 @@ const server = Bun.serve<WebSocketData>({
|
||||
}
|
||||
|
||||
const page = pages[url.pathname];
|
||||
if (page) {
|
||||
const file = Bun.file(page);
|
||||
if (!(await file.exists())) {
|
||||
return new Response("Not Found", { status: 404 });
|
||||
}
|
||||
return new Response(Bun.file(page));
|
||||
}
|
||||
if (page) return serveFile(page);
|
||||
|
||||
if (
|
||||
url.pathname.startsWith("/client/") ||
|
||||
@@ -47,7 +47,7 @@ const server = Bun.serve<WebSocketData>({
|
||||
if (url.pathname.includes("..")) {
|
||||
return new Response("Bad path", { status: 400 });
|
||||
}
|
||||
return new Response(Bun.file("." + url.pathname));
|
||||
return serveFile("." + url.pathname);
|
||||
}
|
||||
|
||||
if (url.pathname === "/styles.css")
|
||||
@@ -66,7 +66,23 @@ const server = Bun.serve<WebSocketData>({
|
||||
console.log("A client connect via websocket!");
|
||||
},
|
||||
message(ws, message) {
|
||||
const data = JSON.parse(message as string);
|
||||
const text = typeof message === "string" ? message : message.toString();
|
||||
console.log("recv:", text);
|
||||
|
||||
let data: any;
|
||||
try {
|
||||
data = JSON.parse(text);
|
||||
} catch {
|
||||
ws.send(JSON.stringify({ type: "ERROR", messsage: "Invalid JSON" }));
|
||||
return;
|
||||
}
|
||||
|
||||
if (data.type === "ECHO") {
|
||||
ws.send(JSON.stringify({ type: "ECHO", text: data.text }));
|
||||
return;
|
||||
}
|
||||
|
||||
// const data = JSON.parse(message as string);
|
||||
if (data.type === "CREATE_ROOM") {
|
||||
const roomCode = Math.floor(1000 + Math.random() * 9000).toString();
|
||||
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
<!doctype html>
|
||||
<meta charset="utf-8" />
|
||||
<title>WebSocket test</title>
|
||||
<button id="send">Send</button>
|
||||
<pre id="log"></pre>
|
||||
|
||||
<script>
|
||||
const logEl = document.getElementById("log");
|
||||
const log = (msg) => (logEl.textContent += msg + "\n");
|
||||
const wsProto = location.protocol === "https:" ? "wss:" : "ws:";
|
||||
const ws = new WebSocket(`${wsProto}//${location.host}/ws`);
|
||||
|
||||
ws.onopen = () => {
|
||||
log("open");
|
||||
ws.send(JSON.stringify({ type: "ECHO", text: "hello" }));
|
||||
};
|
||||
|
||||
ws.onmessage = (event) => log("recv: " + event.data);
|
||||
ws.onclose = (event) => log(`close ${event.code}`);
|
||||
|
||||
document.getElementById("send").onclick = () => {
|
||||
ws.send(JSON.stringify({ type: "ECHO", text: "button " }));
|
||||
};
|
||||
</script>
|
||||
Reference in new issue
Block a user