prepare websockets
This commit is contained in:
@@ -8,8 +8,11 @@ const errorPage = "404.html";
|
|||||||
// SERVER
|
// SERVER
|
||||||
const server = Bun.serve({
|
const server = Bun.serve({
|
||||||
fetch(request, server) {
|
fetch(request, server) {
|
||||||
const requestPath = new URL(request.url).pathname;
|
// allow websockets
|
||||||
|
server.upgrade(request);
|
||||||
|
|
||||||
|
// respond
|
||||||
|
const requestPath = new URL(request.url).pathname;
|
||||||
try {
|
try {
|
||||||
switch (requestPath) {
|
switch (requestPath) {
|
||||||
case "/":
|
case "/":
|
||||||
@@ -21,6 +24,16 @@ const server = Bun.serve({
|
|||||||
return createFileResponse(errorPage);
|
return createFileResponse(errorPage);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
|
websocket: {
|
||||||
|
open(ws) {
|
||||||
|
|
||||||
|
},
|
||||||
|
message(ws, message) {
|
||||||
|
|
||||||
|
},
|
||||||
|
close(ws, code, reason) {},
|
||||||
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
function createFileResponse(requestPath: string): Response {
|
function createFileResponse(requestPath: string): Response {
|
||||||
|
|||||||
Vendored
+4
-3
@@ -57,7 +57,8 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
// src/index.tsx
|
// src/index.tsx
|
||||||
document.querySelector("main").append(
|
var ws = new WebSocket(`ws://${window.location.host}`);
|
||||||
/* @__PURE__ */ createElement("h1", null, "Hello there!")
|
ws.onopen = () => ws.send("Hello!");
|
||||||
);
|
ws.onmessage = (data) => console.log(data);
|
||||||
|
document.querySelector("main").append(/* @__PURE__ */ createElement("h1", null, "Hello there!"));
|
||||||
})();
|
})();
|
||||||
|
|||||||
@@ -1,5 +1,8 @@
|
|||||||
import * as React from "bloatless-react";
|
import * as React from "bloatless-react";
|
||||||
|
|
||||||
document.querySelector("main")!.append(
|
const ws = new WebSocket(`ws://${window.location.host}`);
|
||||||
<h1>Hello there!</h1>
|
|
||||||
);
|
ws.onopen = () => ws.send("Hello!")
|
||||||
|
ws.onmessage = (data) => console.log(data);
|
||||||
|
|
||||||
|
document.querySelector("main")!.append(<h1>Hello there!</h1>);
|
||||||
|
|||||||
Reference in New Issue
Block a user