diff --git a/index.ts b/index.ts index ecb6448..06c10b8 100644 --- a/index.ts +++ b/index.ts @@ -8,8 +8,11 @@ const errorPage = "404.html"; // SERVER const server = Bun.serve({ fetch(request, server) { - const requestPath = new URL(request.url).pathname; + // allow websockets + server.upgrade(request); + // respond + const requestPath = new URL(request.url).pathname; try { switch (requestPath) { case "/": @@ -21,6 +24,16 @@ const server = Bun.serve({ return createFileResponse(errorPage); } }, + + websocket: { + open(ws) { + + }, + message(ws, message) { + + }, + close(ws, code, reason) {}, + }, }); function createFileResponse(requestPath: string): Response { diff --git a/static/dist/index.js b/static/dist/index.js index c847fd0..c6ddc55 100644 --- a/static/dist/index.js +++ b/static/dist/index.js @@ -57,7 +57,8 @@ } // src/index.tsx - document.querySelector("main").append( - /* @__PURE__ */ createElement("h1", null, "Hello there!") - ); + var ws = new WebSocket(`ws://${window.location.host}`); + ws.onopen = () => ws.send("Hello!"); + ws.onmessage = (data) => console.log(data); + document.querySelector("main").append(/* @__PURE__ */ createElement("h1", null, "Hello there!")); })(); diff --git a/static/src/index.tsx b/static/src/index.tsx index 7fda431..9cb40d2 100644 --- a/static/src/index.tsx +++ b/static/src/index.tsx @@ -1,5 +1,8 @@ import * as React from "bloatless-react"; -document.querySelector("main")!.append( -