Spaces:
Sleeping
Sleeping
Update app.js
Browse files
app.js
CHANGED
|
@@ -1,6 +1,5 @@
|
|
| 1 |
-
// server.js (Bun)
|
| 2 |
-
// import { serve } from "bun";
|
| 3 |
|
|
|
|
| 4 |
const rooms = new Map(); // roomId ⇒ Set<ServerWebSocket>
|
| 5 |
|
| 6 |
Bun.serve({
|
|
@@ -9,10 +8,29 @@ Bun.serve({
|
|
| 9 |
fetch(req, server) {
|
| 10 |
const url = new URL(req.url);
|
| 11 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
// Upgrade to WebSocket if requested
|
| 13 |
if (req.headers.get("upgrade")?.toLowerCase() === "websocket") {
|
| 14 |
-
// Accept the upgrade
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
}
|
| 17 |
|
| 18 |
// Serve HTML UI for non-WebSocket requests
|
|
@@ -41,10 +59,10 @@ Bun.serve({
|
|
| 41 |
ws = new WebSocket(socketUrl);
|
| 42 |
ws.onopen = function(){ log('🔌 Connected'); };
|
| 43 |
ws.onmessage = function(ev){
|
| 44 |
-
var m=JSON.parse(ev.data);
|
| 45 |
-
log('['+m.roomId+'] '+m.message);
|
| 46 |
};
|
| 47 |
-
ws.onerror = function(
|
| 48 |
ws.onclose = function(c){ log('❌ Disconnected (code='+c.code+')'); };
|
| 49 |
});
|
| 50 |
function joinRoom(){
|
|
@@ -61,16 +79,23 @@ Bun.serve({
|
|
| 61 |
document.getElementById('msg').value = '';
|
| 62 |
}
|
| 63 |
function log(txt){
|
| 64 |
-
var e=document.getElementById('log');
|
| 65 |
-
e.innerHTML+='<div>'+txt+'</div>';
|
|
|
|
| 66 |
}
|
| 67 |
</script>
|
| 68 |
</body></html>`, {
|
| 69 |
-
headers: {
|
|
|
|
|
|
|
|
|
|
| 70 |
});
|
| 71 |
}
|
| 72 |
|
| 73 |
-
return new Response("Not Found", {
|
|
|
|
|
|
|
|
|
|
| 74 |
},
|
| 75 |
|
| 76 |
websocket: {
|
|
@@ -99,12 +124,13 @@ Bun.serve({
|
|
| 99 |
timestamp: Date.now(),
|
| 100 |
});
|
| 101 |
for (const client of set) {
|
| 102 |
-
if (client.readyState === 1) { //
|
| 103 |
client.send(payload);
|
| 104 |
}
|
| 105 |
}
|
| 106 |
}
|
| 107 |
},
|
|
|
|
| 108 |
close(ws) {
|
| 109 |
// Clean up on disconnect
|
| 110 |
for (const set of rooms.values()) set.delete(ws);
|
|
@@ -112,4 +138,121 @@ Bun.serve({
|
|
| 112 |
}
|
| 113 |
});
|
| 114 |
|
| 115 |
-
console.log("✅ Bun realtime server running on port " + (Bun.env.PORT || 7860));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
|
| 2 |
+
// server.js (Bun) — with CORS enabled for WS and HTTP
|
| 3 |
const rooms = new Map(); // roomId ⇒ Set<ServerWebSocket>
|
| 4 |
|
| 5 |
Bun.serve({
|
|
|
|
| 8 |
fetch(req, server) {
|
| 9 |
const url = new URL(req.url);
|
| 10 |
|
| 11 |
+
// Handle CORS preflight
|
| 12 |
+
if (req.method === "OPTIONS") {
|
| 13 |
+
return new Response(null, {
|
| 14 |
+
status: 204,
|
| 15 |
+
headers: {
|
| 16 |
+
"Access-Control-Allow-Origin": "*",
|
| 17 |
+
"Access-Control-Allow-Methods": "GET, POST, OPTIONS",
|
| 18 |
+
"Access-Control-Allow-Headers": "Content-Type, Upgrade",
|
| 19 |
+
"Access-Control-Allow-Credentials": "true",
|
| 20 |
+
},
|
| 21 |
+
});
|
| 22 |
+
}
|
| 23 |
+
|
| 24 |
// Upgrade to WebSocket if requested
|
| 25 |
if (req.headers.get("upgrade")?.toLowerCase() === "websocket") {
|
| 26 |
+
// Accept the upgrade with CORS headers on the handshake
|
| 27 |
+
const upgradeRes = server.upgrade(req, {
|
| 28 |
+
headers: {
|
| 29 |
+
"Access-Control-Allow-Origin": "*",
|
| 30 |
+
"Access-Control-Allow-Credentials": "true",
|
| 31 |
+
},
|
| 32 |
+
});
|
| 33 |
+
return upgradeRes ?? new Response("Upgrade failed", { status: 500 });
|
| 34 |
}
|
| 35 |
|
| 36 |
// Serve HTML UI for non-WebSocket requests
|
|
|
|
| 59 |
ws = new WebSocket(socketUrl);
|
| 60 |
ws.onopen = function(){ log('🔌 Connected'); };
|
| 61 |
ws.onmessage = function(ev){
|
| 62 |
+
var m = JSON.parse(ev.data);
|
| 63 |
+
log('[' + m.roomId + '] ' + m.message);
|
| 64 |
};
|
| 65 |
+
ws.onerror = function(){ log('⚠️ WebSocket error'); };
|
| 66 |
ws.onclose = function(c){ log('❌ Disconnected (code='+c.code+')'); };
|
| 67 |
});
|
| 68 |
function joinRoom(){
|
|
|
|
| 79 |
document.getElementById('msg').value = '';
|
| 80 |
}
|
| 81 |
function log(txt){
|
| 82 |
+
var e = document.getElementById('log');
|
| 83 |
+
e.innerHTML += '<div>'+txt+'</div>';
|
| 84 |
+
e.scrollTop = e.scrollHeight;
|
| 85 |
}
|
| 86 |
</script>
|
| 87 |
</body></html>`, {
|
| 88 |
+
headers: {
|
| 89 |
+
"Content-Type": "text/html; charset=utf-8",
|
| 90 |
+
"Access-Control-Allow-Origin": "*",
|
| 91 |
+
}
|
| 92 |
});
|
| 93 |
}
|
| 94 |
|
| 95 |
+
return new Response("Not Found", {
|
| 96 |
+
status: 404,
|
| 97 |
+
headers: { "Access-Control-Allow-Origin": "*" },
|
| 98 |
+
});
|
| 99 |
},
|
| 100 |
|
| 101 |
websocket: {
|
|
|
|
| 124 |
timestamp: Date.now(),
|
| 125 |
});
|
| 126 |
for (const client of set) {
|
| 127 |
+
if (client.readyState === 1) { // OPEN
|
| 128 |
client.send(payload);
|
| 129 |
}
|
| 130 |
}
|
| 131 |
}
|
| 132 |
},
|
| 133 |
+
|
| 134 |
close(ws) {
|
| 135 |
// Clean up on disconnect
|
| 136 |
for (const set of rooms.values()) set.delete(ws);
|
|
|
|
| 138 |
}
|
| 139 |
});
|
| 140 |
|
| 141 |
+
console.log("✅ Bun realtime server running on port " + (Bun.env.PORT || 7860));
|
| 142 |
+
|
| 143 |
+
|
| 144 |
+
// // server.js (Bun)
|
| 145 |
+
// // import { serve } from "bun";
|
| 146 |
+
|
| 147 |
+
// const rooms = new Map(); // roomId ⇒ Set<ServerWebSocket>
|
| 148 |
+
|
| 149 |
+
// Bun.serve({
|
| 150 |
+
// port: Number(Bun.env.PORT) || 7860,
|
| 151 |
+
|
| 152 |
+
// fetch(req, server) {
|
| 153 |
+
// const url = new URL(req.url);
|
| 154 |
+
|
| 155 |
+
// // Upgrade to WebSocket if requested
|
| 156 |
+
// if (req.headers.get("upgrade")?.toLowerCase() === "websocket") {
|
| 157 |
+
// // Accept the upgrade and attach room data if needed
|
| 158 |
+
// return server.upgrade(req) ? undefined : new Response("Upgrade failed", { status: 500 });
|
| 159 |
+
// }
|
| 160 |
+
|
| 161 |
+
// // Serve HTML UI for non-WebSocket requests
|
| 162 |
+
// if (url.pathname === "/") {
|
| 163 |
+
// return new Response(`<!doctype html>
|
| 164 |
+
// <html><head>
|
| 165 |
+
// <title>Dubem Realtime Rooms</title>
|
| 166 |
+
// <meta charset="utf-8"/>
|
| 167 |
+
// <style>
|
| 168 |
+
// body { font-family: sans-serif; padding: 20px; background: #f0f0f0; }
|
| 169 |
+
// #log { border: 1px solid #ccc; height:200px; overflow-y:auto; padding:10px; background:#fff;}
|
| 170 |
+
// input, button { margin:5px 0; padding:8px; font-size:16px; }
|
| 171 |
+
// </style>
|
| 172 |
+
// </head><body>
|
| 173 |
+
// <h2>Join a Room & Send Messages</h2>
|
| 174 |
+
// <input id="room" placeholder="Room ID"/>
|
| 175 |
+
// <button onclick="joinRoom()">Join Room</button>
|
| 176 |
+
// <div id="log"></div>
|
| 177 |
+
// <input id="msg" placeholder="Type a message" style="width:80%;"/>
|
| 178 |
+
// <button onclick="sendMsg()">Send</button>
|
| 179 |
+
// <script>
|
| 180 |
+
// var ws, currentRoom;
|
| 181 |
+
// window.addEventListener('load', function() {
|
| 182 |
+
// var scheme = location.protocol === 'https:' ? 'wss' : 'ws';
|
| 183 |
+
// var socketUrl = scheme + '://' + location.host + '/';
|
| 184 |
+
// ws = new WebSocket(socketUrl);
|
| 185 |
+
// ws.onopen = function(){ log('🔌 Connected'); };
|
| 186 |
+
// ws.onmessage = function(ev){
|
| 187 |
+
// var m=JSON.parse(ev.data);
|
| 188 |
+
// log('['+m.roomId+'] '+m.message);
|
| 189 |
+
// };
|
| 190 |
+
// ws.onerror = function(e){ log('⚠️ WebSocket error'); };
|
| 191 |
+
// ws.onclose = function(c){ log('❌ Disconnected (code='+c.code+')'); };
|
| 192 |
+
// });
|
| 193 |
+
// function joinRoom(){
|
| 194 |
+
// var id = document.getElementById('room').value.trim();
|
| 195 |
+
// if(!id) return alert('Enter room ID');
|
| 196 |
+
// ws.send(JSON.stringify({ action:'join', roomId:id }));
|
| 197 |
+
// currentRoom = id; log('➡️ Joined '+id);
|
| 198 |
+
// }
|
| 199 |
+
// function sendMsg(){
|
| 200 |
+
// var t = document.getElementById('msg').value.trim();
|
| 201 |
+
// if(!t) return;
|
| 202 |
+
// if(!currentRoom) return alert('Join a room first');
|
| 203 |
+
// ws.send(JSON.stringify({ action:'post', roomId:currentRoom, message:t }));
|
| 204 |
+
// document.getElementById('msg').value = '';
|
| 205 |
+
// }
|
| 206 |
+
// function log(txt){
|
| 207 |
+
// var e=document.getElementById('log');
|
| 208 |
+
// e.innerHTML+='<div>'+txt+'</div>'; e.scrollTop=e.scrollHeight;
|
| 209 |
+
// }
|
| 210 |
+
// </script>
|
| 211 |
+
// </body></html>`, {
|
| 212 |
+
// headers: { "Content-Type": "text/html; charset=utf-8" }
|
| 213 |
+
// });
|
| 214 |
+
// }
|
| 215 |
+
|
| 216 |
+
// return new Response("Not Found", { status: 404 });
|
| 217 |
+
// },
|
| 218 |
+
|
| 219 |
+
// websocket: {
|
| 220 |
+
// message(ws, raw) {
|
| 221 |
+
// let msg;
|
| 222 |
+
// try { msg = JSON.parse(raw); } catch { return; }
|
| 223 |
+
|
| 224 |
+
// if (msg.action === "join" && msg.roomId) {
|
| 225 |
+
// // Leave all rooms
|
| 226 |
+
// for (const set of rooms.values()) set.delete(ws);
|
| 227 |
+
// // Join target room
|
| 228 |
+
// let set = rooms.get(msg.roomId);
|
| 229 |
+
// if (!set) {
|
| 230 |
+
// set = new Set();
|
| 231 |
+
// rooms.set(msg.roomId, set);
|
| 232 |
+
// }
|
| 233 |
+
// set.add(ws);
|
| 234 |
+
// }
|
| 235 |
+
|
| 236 |
+
// if (msg.action === "post" && msg.roomId && msg.message) {
|
| 237 |
+
// const set = rooms.get(msg.roomId);
|
| 238 |
+
// if (!set) return;
|
| 239 |
+
// const payload = JSON.stringify({
|
| 240 |
+
// roomId: msg.roomId,
|
| 241 |
+
// message: msg.message,
|
| 242 |
+
// timestamp: Date.now(),
|
| 243 |
+
// });
|
| 244 |
+
// for (const client of set) {
|
| 245 |
+
// if (client.readyState === 1) { // 1 === OPEN
|
| 246 |
+
// client.send(payload);
|
| 247 |
+
// }
|
| 248 |
+
// }
|
| 249 |
+
// }
|
| 250 |
+
// },
|
| 251 |
+
// close(ws) {
|
| 252 |
+
// // Clean up on disconnect
|
| 253 |
+
// for (const set of rooms.values()) set.delete(ws);
|
| 254 |
+
// }
|
| 255 |
+
// }
|
| 256 |
+
// });
|
| 257 |
+
|
| 258 |
+
// console.log("✅ Bun realtime server running on port " + (Bun.env.PORT || 7860));
|