Spaces:
Sleeping
Sleeping
Update app.js
Browse files
app.js
CHANGED
|
@@ -1,72 +1,64 @@
|
|
| 1 |
-
|
| 2 |
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
fetch(req, server) {
|
| 6 |
-
const url = new URL(req.url);
|
| 7 |
|
| 8 |
-
|
| 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 |
-
|
| 21 |
-
|
| 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 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
}
|
| 46 |
-
|
| 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 |
-
|
| 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
|
|
|
|
| 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.");
|