Spaces:
Sleeping
Sleeping
Update app.js
Browse files
app.js
CHANGED
|
@@ -1,8 +1,7 @@
|
|
| 1 |
-
const rooms = new Map();
|
| 2 |
|
| 3 |
Bun.serve({
|
| 4 |
port: 7860,
|
| 5 |
-
|
| 6 |
fetch(req, server) {
|
| 7 |
const url = new URL(req.url);
|
| 8 |
|
|
@@ -20,18 +19,17 @@ Bun.serve({
|
|
| 20 |
|
| 21 |
// WebSocket upgrade
|
| 22 |
if (req.headers.get("upgrade")?.toLowerCase() === "websocket") {
|
|
|
|
| 23 |
const success = server.upgrade(req);
|
| 24 |
-
if (success) return;
|
| 25 |
return new Response("Upgrade failed", { status: 500 });
|
| 26 |
}
|
| 27 |
|
| 28 |
-
// Serve
|
| 29 |
if (url.pathname === "/") {
|
| 30 |
const html = `<!DOCTYPE html>
|
| 31 |
<html><head><meta charset="utf-8"><title>Host Test</title></head><body>
|
| 32 |
<h2>Host Server</h2>
|
| 33 |
-
Room: <input id="room" placeholder="room"/><button onclick="join()">Join</button><br>
|
| 34 |
-
Message: <input id="msg"/><button onclick="send()">Send</button>
|
| 35 |
<pre id="log"></pre>
|
| 36 |
<script>
|
| 37 |
let ws;
|
|
@@ -42,12 +40,6 @@ Bun.serve({
|
|
| 42 |
ws.onclose = () => setTimeout(connect, 1000);
|
| 43 |
}
|
| 44 |
connect();
|
| 45 |
-
function join() {
|
| 46 |
-
ws.send(JSON.stringify({ action:"join", room: document.getElementById("room").value }));
|
| 47 |
-
}
|
| 48 |
-
function send() {
|
| 49 |
-
ws.send(JSON.stringify({ action:"post", room: document.getElementById("room").value, message: document.getElementById("msg").value }));
|
| 50 |
-
}
|
| 51 |
function log(txt) {
|
| 52 |
document.getElementById("log").textContent += txt + "\\n";
|
| 53 |
}
|
|
@@ -63,35 +55,16 @@ Bun.serve({
|
|
| 63 |
|
| 64 |
return new Response("Not Found", { status: 404, headers: { "Access-Control-Allow-Origin": "*" } });
|
| 65 |
},
|
| 66 |
-
|
| 67 |
websocket: {
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
message(ws, raw) {
|
| 69 |
-
|
| 70 |
-
try { msg = JSON.parse(raw); } catch { return; }
|
| 71 |
-
const { action, room, message } = msg;
|
| 72 |
-
if (action === "join" && room) {
|
| 73 |
-
for (const set of rooms.values()) set.delete(ws);
|
| 74 |
-
let set = rooms.get(room);
|
| 75 |
-
if (!set) { set = new Set(); rooms.set(room, set); }
|
| 76 |
-
set.add(ws);
|
| 77 |
-
ws.send(JSON.stringify({ system: `Joined room ${room}` }));
|
| 78 |
-
}
|
| 79 |
-
if (action === "post" && room && message) {
|
| 80 |
-
const set = rooms.get(room);
|
| 81 |
-
if (!set) return;
|
| 82 |
-
const payload = JSON.stringify({
|
| 83 |
-
room,
|
| 84 |
-
message,
|
| 85 |
-
from: "host",
|
| 86 |
-
timestamp: Date.now(),
|
| 87 |
-
});
|
| 88 |
-
for (const client of set) {
|
| 89 |
-
if (client.readyState === 1) client.send(payload);
|
| 90 |
-
}
|
| 91 |
-
}
|
| 92 |
},
|
| 93 |
close(ws) {
|
| 94 |
-
|
| 95 |
},
|
| 96 |
},
|
| 97 |
});
|
|
|
|
| 1 |
+
const rooms = new Map();
|
| 2 |
|
| 3 |
Bun.serve({
|
| 4 |
port: 7860,
|
|
|
|
| 5 |
fetch(req, server) {
|
| 6 |
const url = new URL(req.url);
|
| 7 |
|
|
|
|
| 19 |
|
| 20 |
// WebSocket upgrade
|
| 21 |
if (req.headers.get("upgrade")?.toLowerCase() === "websocket") {
|
| 22 |
+
// Only call upgrade, do not return anything else
|
| 23 |
const success = server.upgrade(req);
|
| 24 |
+
if (success) return;
|
| 25 |
return new Response("Upgrade failed", { status: 500 });
|
| 26 |
}
|
| 27 |
|
| 28 |
+
// Serve HTML
|
| 29 |
if (url.pathname === "/") {
|
| 30 |
const html = `<!DOCTYPE html>
|
| 31 |
<html><head><meta charset="utf-8"><title>Host Test</title></head><body>
|
| 32 |
<h2>Host Server</h2>
|
|
|
|
|
|
|
| 33 |
<pre id="log"></pre>
|
| 34 |
<script>
|
| 35 |
let ws;
|
|
|
|
| 40 |
ws.onclose = () => setTimeout(connect, 1000);
|
| 41 |
}
|
| 42 |
connect();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
function log(txt) {
|
| 44 |
document.getElementById("log").textContent += txt + "\\n";
|
| 45 |
}
|
|
|
|
| 55 |
|
| 56 |
return new Response("Not Found", { status: 404, headers: { "Access-Control-Allow-Origin": "*" } });
|
| 57 |
},
|
|
|
|
| 58 |
websocket: {
|
| 59 |
+
open(ws) {
|
| 60 |
+
// Optionally notify client on open
|
| 61 |
+
ws.send(JSON.stringify({ system: "WebSocket connection established" }));
|
| 62 |
+
},
|
| 63 |
message(ws, raw) {
|
| 64 |
+
ws.send(raw); // echo for demo
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
},
|
| 66 |
close(ws) {
|
| 67 |
+
// Cleanup logic if needed
|
| 68 |
},
|
| 69 |
},
|
| 70 |
});
|