Pepguy commited on
Commit
0acab33
·
verified ·
1 Parent(s): ffc4dd7

Update app.js

Browse files
Files changed (1) hide show
  1. app.js +57 -65
app.js CHANGED
@@ -1,72 +1,64 @@
1
- const rooms = new Map();
2
 
3
- Bun.serve({
4
- port: 7860,
5
- fetch(req, server) {
6
- const url = new URL(req.url);
7
 
8
- // CORS preflight
9
- if (req.method === "OPTIONS") {
10
- return new Response(null, {
11
- status: 204,
12
- headers: {
13
- "Access-Control-Allow-Origin": "*",
14
- "Access-Control-Allow-Methods": "GET,POST,OPTIONS",
15
- "Access-Control-Allow-Headers": "Content-Type,Upgrade",
16
- },
17
- });
18
- }
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;
36
- function connect() {
37
- ws = new WebSocket("ws://" + location.host);
38
- ws.onopen = () => log("🔌 connected");
39
- ws.onmessage = e => log(e.data);
40
- ws.onclose = () => setTimeout(connect, 1000);
41
- }
42
- connect();
43
- function log(txt) {
44
- document.getElementById("log").textContent += txt + "\\n";
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
45
  }
46
- </script>
47
- </body></html>`;
48
- return new Response(html, {
49
- headers: {
50
- "Content-Type": "text/html; charset=utf-8",
51
- "Access-Control-Allow-Origin": "*",
52
- },
53
- });
54
- }
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
- });
71
 
72
- console.log("✅ Host server running on ws://localhost:7860");
 
1
+ // host.js (Bun)
2
 
3
+ const DELIVERY_SERVER_URL = "wss://pepguy-activity-delivery.hf.space/";
4
+ const HOST_SECRET = "a-very-secret-host-key"; // Must match the delivery server
 
 
5
 
6
+ let deliverySocket;
 
 
 
 
 
 
 
 
 
 
7
 
8
+ function connectToDeliveryServer() {
9
+ console.log(`Attempting to connect to delivery server at ${DELIVERY_SERVER_URL}...`);
 
 
 
 
 
10
 
11
+ try {
12
+ deliverySocket = new WebSocket(DELIVERY_SERVER_URL, {
13
+ headers: { "X-Host-Secret": HOST_SECRET },
14
+ });
15
+
16
+ deliverySocket.onopen = () => {
17
+ console.log("✅ Successfully connected to delivery server.");
18
+ };
19
+
20
+ deliverySocket.onmessage = (event) => {
21
+ let msg;
22
+ try {
23
+ msg = JSON.parse(event.data);
24
+ } catch {
25
+ return;
26
+ }
27
+
28
+ if (msg.action === "post" && msg.roomId && msg.message) {
29
+ const finalPayload = JSON.stringify({
30
+ roomId: msg.roomId,
31
+ message: msg.message,
32
+ timestamp: Date.now(),
33
+ });
34
+
35
+ // The instruction for the delivery server is to 'broadcast' this payload
36
+ const broadcastInstruction = JSON.stringify({
37
+ roomId: msg.roomId,
38
+ payload: finalPayload,
39
+ });
40
+
41
+ if (deliverySocket.readyState === 1) {
42
+ deliverySocket.send(broadcastInstruction);
43
+ }
44
+ }
45
+ };
46
+
47
+ deliverySocket.onclose = (event) => {
48
+ console.error(`❌ Disconnected from delivery server. Code: ${event.code}. Reconnecting in 5s...`);
49
+ setTimeout(connectToDeliveryServer, 5000);
50
+ };
51
+
52
+ deliverySocket.onerror = (error) => {
53
+ console.error("WebSocket error:", error.message);
54
+ };
55
+
56
+ } catch (error) {
57
+ console.error("Failed to initiate connection:", error.message);
58
+ setTimeout(connectToDeliveryServer, 5000);
59
  }
60
+ }
 
 
 
 
 
 
 
 
61
 
62
+ connectToDeliveryServer();
 
 
 
 
 
 
 
 
 
 
 
 
 
 
63
 
64
+ console.log("✅ Host server logic is running.");