Spaces:
Sleeping
Sleeping
Update app.js
Browse files
app.js
CHANGED
|
@@ -1,6 +1,5 @@
|
|
| 1 |
-
|
| 2 |
-
// server.js (Bun) — with CORS enabled for WS and HTTP
|
| 3 |
const rooms = new Map(); // roomId ⇒ Set<ServerWebSocket>
|
|
|
|
| 4 |
|
| 5 |
Bun.serve({
|
| 6 |
port: Number(Bun.env.PORT) || 7860,
|
|
@@ -15,7 +14,7 @@ Bun.serve({
|
|
| 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 |
});
|
|
@@ -23,6 +22,11 @@ Bun.serve({
|
|
| 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: {
|
|
@@ -48,15 +52,28 @@ Bun.serve({
|
|
| 48 |
<h2>Join a Room & Send Messages</h2>
|
| 49 |
<input id="room" placeholder="Room ID"/>
|
| 50 |
<button onclick="joinRoom()">Join Room</button>
|
|
|
|
|
|
|
|
|
|
| 51 |
<div id="log"></div>
|
| 52 |
<input id="msg" placeholder="Type a message" style="width:80%;"/>
|
| 53 |
<button onclick="sendMsg()">Send</button>
|
| 54 |
<script>
|
| 55 |
var ws, currentRoom;
|
| 56 |
-
|
| 57 |
var scheme = location.protocol === 'https:' ? 'wss' : 'ws';
|
| 58 |
var socketUrl = scheme + '://' + location.host + '/';
|
| 59 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
ws.onopen = function(){ log('🔌 Connected'); };
|
| 61 |
ws.onmessage = function(ev){
|
| 62 |
var m = JSON.parse(ev.data);
|
|
@@ -64,7 +81,9 @@ Bun.serve({
|
|
| 64 |
};
|
| 65 |
ws.onerror = function(){ log('⚠️ WebSocket error'); };
|
| 66 |
ws.onclose = function(c){ log('❌ Disconnected (code='+c.code+')'); };
|
| 67 |
-
}
|
|
|
|
|
|
|
| 68 |
function joinRoom(){
|
| 69 |
var id = document.getElementById('room').value.trim();
|
| 70 |
if(!id) return alert('Enter room ID');
|
|
@@ -138,121 +157,4 @@ Bun.serve({
|
|
| 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));
|
|
|
|
|
|
|
|
|
|
| 1 |
const rooms = new Map(); // roomId ⇒ Set<ServerWebSocket>
|
| 2 |
+
const HARDCODED_TOKEN = "test-token-123";
|
| 3 |
|
| 4 |
Bun.serve({
|
| 5 |
port: Number(Bun.env.PORT) || 7860,
|
|
|
|
| 14 |
headers: {
|
| 15 |
"Access-Control-Allow-Origin": "*",
|
| 16 |
"Access-Control-Allow-Methods": "GET, POST, OPTIONS",
|
| 17 |
+
"Access-Control-Allow-Headers": "Content-Type, Upgrade, Authorization",
|
| 18 |
"Access-Control-Allow-Credentials": "true",
|
| 19 |
},
|
| 20 |
});
|
|
|
|
| 22 |
|
| 23 |
// Upgrade to WebSocket if requested
|
| 24 |
if (req.headers.get("upgrade")?.toLowerCase() === "websocket") {
|
| 25 |
+
// Bearer token check
|
| 26 |
+
const auth = req.headers.get("authorization");
|
| 27 |
+
if (auth !== `Bearer ${HARDCODED_TOKEN}`) {
|
| 28 |
+
return new Response("Unauthorized", { status: 401, headers: { "Access-Control-Allow-Origin": "*" } });
|
| 29 |
+
}
|
| 30 |
// Accept the upgrade with CORS headers on the handshake
|
| 31 |
const upgradeRes = server.upgrade(req, {
|
| 32 |
headers: {
|
|
|
|
| 52 |
<h2>Join a Room & Send Messages</h2>
|
| 53 |
<input id="room" placeholder="Room ID"/>
|
| 54 |
<button onclick="joinRoom()">Join Room</button>
|
| 55 |
+
<div>
|
| 56 |
+
<input type="checkbox" id="sendToken" checked>Send Bearer Token
|
| 57 |
+
</div>
|
| 58 |
<div id="log"></div>
|
| 59 |
<input id="msg" placeholder="Type a message" style="width:80%;"/>
|
| 60 |
<button onclick="sendMsg()">Send</button>
|
| 61 |
<script>
|
| 62 |
var ws, currentRoom;
|
| 63 |
+
function connectWS() {
|
| 64 |
var scheme = location.protocol === 'https:' ? 'wss' : 'ws';
|
| 65 |
var socketUrl = scheme + '://' + location.host + '/';
|
| 66 |
+
var sendToken = document.getElementById('sendToken').checked;
|
| 67 |
+
if (ws && ws.readyState === 1) ws.close();
|
| 68 |
+
// Bun supports custom headers in WebSocket constructor, browsers do not!
|
| 69 |
+
try {
|
| 70 |
+
ws = sendToken
|
| 71 |
+
? new WebSocket(socketUrl, { headers: { "Authorization": "Bearer test-token-123" } })
|
| 72 |
+
: new WebSocket(socketUrl);
|
| 73 |
+
} catch (e) {
|
| 74 |
+
log('Custom headers not supported in this environment.');
|
| 75 |
+
ws = new WebSocket(socketUrl);
|
| 76 |
+
}
|
| 77 |
ws.onopen = function(){ log('🔌 Connected'); };
|
| 78 |
ws.onmessage = function(ev){
|
| 79 |
var m = JSON.parse(ev.data);
|
|
|
|
| 81 |
};
|
| 82 |
ws.onerror = function(){ log('⚠️ WebSocket error'); };
|
| 83 |
ws.onclose = function(c){ log('❌ Disconnected (code='+c.code+')'); };
|
| 84 |
+
}
|
| 85 |
+
window.addEventListener('load', connectWS);
|
| 86 |
+
document.getElementById('sendToken').addEventListener('change', connectWS);
|
| 87 |
function joinRoom(){
|
| 88 |
var id = document.getElementById('room').value.trim();
|
| 89 |
if(!id) return alert('Enter room ID');
|
|
|
|
| 157 |
}
|
| 158 |
});
|
| 159 |
|
| 160 |
+
console.log("✅ Bun realtime server running on port " + (Bun.env.PORT || 7860));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|