Spaces:
Sleeping
Sleeping
Update app.js
Browse files
app.js
CHANGED
|
@@ -1,133 +1,100 @@
|
|
| 1 |
-
//
|
|
|
|
|
|
|
| 2 |
const rooms = new Map(); // roomId ⇒ Set<ServerWebSocket>
|
| 3 |
-
const VALID_TOKEN = "mysecrettoken"; // ← your hard-coded secret
|
| 4 |
|
| 5 |
-
|
| 6 |
-
port:
|
| 7 |
|
| 8 |
fetch(req, server) {
|
| 9 |
const url = new URL(req.url);
|
| 10 |
|
| 11 |
-
//
|
| 12 |
if (req.method === "OPTIONS") {
|
| 13 |
-
return new Response(null, {
|
| 14 |
-
|
| 15 |
-
"Access-Control-Allow-Methods": "GET, POST, OPTIONS",
|
| 16 |
-
"Access-Control-Allow-Headers": "Content-Type, Upgrade",
|
| 17 |
-
"Access-Control-Allow-Credentials": "true",
|
| 18 |
-
}});
|
| 19 |
-
}
|
| 20 |
-
|
| 21 |
-
// Upgrade to WebSocket if requested
|
| 22 |
-
if (req.headers.get("upgrade")?.toLowerCase() === "websocket") {
|
| 23 |
-
// **AUTH CHECK**: require ?token=VALID_TOKEN
|
| 24 |
-
const clientToken = url.searchParams.get("token");
|
| 25 |
-
if (clientToken !== VALID_TOKEN) {
|
| 26 |
-
return new Response("Unauthorized", { status: 401 });
|
| 27 |
-
}
|
| 28 |
-
|
| 29 |
-
// Accept the upgrade with CORS headers on the handshake
|
| 30 |
-
const upgradeRes = server.upgrade(req, {
|
| 31 |
headers: {
|
| 32 |
"Access-Control-Allow-Origin": "*",
|
| 33 |
-
"Access-Control-Allow-
|
|
|
|
| 34 |
},
|
| 35 |
});
|
| 36 |
-
return upgradeRes ?? new Response("Upgrade failed", { status: 500 });
|
| 37 |
}
|
| 38 |
|
| 39 |
-
//
|
| 40 |
-
if (
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
}
|
| 68 |
-
window.addEventListener('load', connect);
|
| 69 |
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
}
|
| 89 |
-
|
| 90 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 91 |
headers: {
|
| 92 |
"Content-Type": "text/html; charset=utf-8",
|
| 93 |
"Access-Control-Allow-Origin": "*",
|
| 94 |
-
}
|
| 95 |
});
|
| 96 |
}
|
| 97 |
|
| 98 |
-
return new Response("Not Found", {
|
| 99 |
-
status: 404,
|
| 100 |
-
headers: { "Access-Control-Allow-Origin": "*" },
|
| 101 |
-
});
|
| 102 |
},
|
| 103 |
-
|
| 104 |
-
websocket: {
|
| 105 |
-
message(ws, raw) {
|
| 106 |
-
let msg;
|
| 107 |
-
try { msg = JSON.parse(raw); } catch { return; }
|
| 108 |
-
if (msg.action === "join" && msg.roomId) {
|
| 109 |
-
for (const set of rooms.values()) set.delete(ws);
|
| 110 |
-
let set = rooms.get(msg.roomId);
|
| 111 |
-
if (!set) { set = new Set(); rooms.set(msg.roomId, set); }
|
| 112 |
-
set.add(ws);
|
| 113 |
-
}
|
| 114 |
-
if (msg.action === "post" && msg.roomId && msg.message) {
|
| 115 |
-
const set = rooms.get(msg.roomId);
|
| 116 |
-
if (!set) return;
|
| 117 |
-
const payload = JSON.stringify({
|
| 118 |
-
roomId: msg.roomId,
|
| 119 |
-
message: msg.message,
|
| 120 |
-
timestamp: Date.now(),
|
| 121 |
-
});
|
| 122 |
-
for (const client of set) {
|
| 123 |
-
if (client.readyState === 1) client.send(payload);
|
| 124 |
-
}
|
| 125 |
-
}
|
| 126 |
-
},
|
| 127 |
-
close(ws) {
|
| 128 |
-
for (const set of rooms.values()) set.delete(ws);
|
| 129 |
-
}
|
| 130 |
-
}
|
| 131 |
});
|
| 132 |
|
| 133 |
-
console.log("✅
|
|
|
|
| 1 |
+
// host.js
|
| 2 |
+
import { serve } from "bun";
|
| 3 |
+
|
| 4 |
const rooms = new Map(); // roomId ⇒ Set<ServerWebSocket>
|
|
|
|
| 5 |
|
| 6 |
+
serve({
|
| 7 |
+
port: 7860, // ← change if you need a different port
|
| 8 |
|
| 9 |
fetch(req, server) {
|
| 10 |
const url = new URL(req.url);
|
| 11 |
|
| 12 |
+
// CORS preflight
|
| 13 |
if (req.method === "OPTIONS") {
|
| 14 |
+
return new Response(null, {
|
| 15 |
+
status: 204,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
headers: {
|
| 17 |
"Access-Control-Allow-Origin": "*",
|
| 18 |
+
"Access-Control-Allow-Methods": "GET,POST,OPTIONS",
|
| 19 |
+
"Access-Control-Allow-Headers": "Content-Type,Upgrade",
|
| 20 |
},
|
| 21 |
});
|
|
|
|
| 22 |
}
|
| 23 |
|
| 24 |
+
// WebSocket upgrade
|
| 25 |
+
if (req.headers.get("upgrade")?.toLowerCase() === "websocket") {
|
| 26 |
+
const ws = server.upgrade(req);
|
| 27 |
+
ws.onmessage = (raw) => {
|
| 28 |
+
let msg;
|
| 29 |
+
try { msg = JSON.parse(raw); } catch { return; }
|
| 30 |
+
const { action, room, message } = msg;
|
| 31 |
+
if (action === "join" && room) {
|
| 32 |
+
// join room
|
| 33 |
+
rooms.get(room)?.delete(ws);
|
| 34 |
+
let set = rooms.get(room);
|
| 35 |
+
if (!set) { set = new Set(); rooms.set(room, set); }
|
| 36 |
+
set.add(ws);
|
| 37 |
+
ws.send(JSON.stringify({ system: `Joined room ${room}` }));
|
| 38 |
+
}
|
| 39 |
+
if (action === "post" && room && message) {
|
| 40 |
+
// broadcast
|
| 41 |
+
const set = rooms.get(room);
|
| 42 |
+
if (!set) return;
|
| 43 |
+
const payload = JSON.stringify({
|
| 44 |
+
room,
|
| 45 |
+
message,
|
| 46 |
+
from: "host",
|
| 47 |
+
timestamp: Date.now(),
|
| 48 |
+
});
|
| 49 |
+
for (const client of set) {
|
| 50 |
+
if (client.readyState === 1) client.send(payload);
|
| 51 |
+
}
|
| 52 |
+
}
|
| 53 |
+
};
|
| 54 |
+
ws.onclose = () => {
|
| 55 |
+
for (const set of rooms.values()) set.delete(ws);
|
| 56 |
+
};
|
| 57 |
+
return ws;
|
| 58 |
}
|
|
|
|
| 59 |
|
| 60 |
+
// Serve inline HTML test page
|
| 61 |
+
if (url.pathname === "/") {
|
| 62 |
+
const html = `<!DOCTYPE html>
|
| 63 |
+
<html><head><meta charset="utf-8"><title>Host Test</title></head><body>
|
| 64 |
+
<h2>Host Server</h2>
|
| 65 |
+
Room: <input id="room" placeholder="room"/><button onclick="join()">Join</button><br>
|
| 66 |
+
Message: <input id="msg"/><button onclick="send()">Send</button>
|
| 67 |
+
<pre id="log"></pre>
|
| 68 |
+
<script>
|
| 69 |
+
let ws;
|
| 70 |
+
function connect() {
|
| 71 |
+
ws = new WebSocket("ws://" + location.host);
|
| 72 |
+
ws.onopen = () => log("🔌 connected");
|
| 73 |
+
ws.onmessage = e => log(e.data);
|
| 74 |
+
ws.onclose = () => setTimeout(connect, 1000);
|
| 75 |
+
}
|
| 76 |
+
connect();
|
| 77 |
+
function join() {
|
| 78 |
+
ws.send(JSON.stringify({ action:"join", room: document.getElementById("room").value }));
|
| 79 |
+
}
|
| 80 |
+
function send() {
|
| 81 |
+
ws.send(JSON.stringify({ action:"post", room: document.getElementById("room").value, message: document.getElementById("msg").value }));
|
| 82 |
+
}
|
| 83 |
+
function log(txt) {
|
| 84 |
+
document.getElementById("log").textContent += txt + "\\n";
|
| 85 |
+
}
|
| 86 |
+
</script>
|
| 87 |
+
</body></html>`;
|
| 88 |
+
return new Response(html, {
|
| 89 |
headers: {
|
| 90 |
"Content-Type": "text/html; charset=utf-8",
|
| 91 |
"Access-Control-Allow-Origin": "*",
|
| 92 |
+
},
|
| 93 |
});
|
| 94 |
}
|
| 95 |
|
| 96 |
+
return new Response("Not Found", { status: 404, headers: { "Access-Control-Allow-Origin": "*" } });
|
|
|
|
|
|
|
|
|
|
| 97 |
},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
});
|
| 99 |
|
| 100 |
+
console.log("✅ Host server running on ws://localhost:7860");
|