Spaces:
Sleeping
Sleeping
Update app.js
Browse files
app.js
CHANGED
|
@@ -1,57 +1,21 @@
|
|
| 1 |
// server.js (Bun)
|
| 2 |
-
import { serve } from "bun";
|
| 3 |
|
| 4 |
-
const rooms = new Map(); // roomId ⇒ Set<
|
| 5 |
|
| 6 |
-
serve({
|
| 7 |
-
port: Number(
|
| 8 |
|
| 9 |
-
|
| 10 |
const url = new URL(req.url);
|
| 11 |
|
| 12 |
-
//
|
| 13 |
if (req.headers.get("upgrade")?.toLowerCase() === "websocket") {
|
| 14 |
-
//
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
socket.onmessage = (raw) => {
|
| 18 |
-
let msg;
|
| 19 |
-
try { msg = JSON.parse(raw.data); } catch { return; }
|
| 20 |
-
|
| 21 |
-
if (msg.action === "join" && msg.roomId) {
|
| 22 |
-
// leave all rooms
|
| 23 |
-
for (const set of rooms.values()) set.delete(socket);
|
| 24 |
-
// join target room
|
| 25 |
-
const set = rooms.get(msg.roomId) || new Set();
|
| 26 |
-
set.add(socket);
|
| 27 |
-
rooms.set(msg.roomId, set);
|
| 28 |
-
}
|
| 29 |
-
|
| 30 |
-
if (msg.action === "post" && msg.roomId && msg.message) {
|
| 31 |
-
const set = rooms.get(msg.roomId);
|
| 32 |
-
if (!set) return;
|
| 33 |
-
const payload = JSON.stringify({
|
| 34 |
-
roomId: msg.roomId,
|
| 35 |
-
message: msg.message,
|
| 36 |
-
timestamp: Date.now(),
|
| 37 |
-
});
|
| 38 |
-
for (const client of set) {
|
| 39 |
-
if (client.readyState === WebSocket.OPEN) {
|
| 40 |
-
client.send(payload);
|
| 41 |
-
}
|
| 42 |
-
}
|
| 43 |
-
}
|
| 44 |
-
};
|
| 45 |
-
|
| 46 |
-
socket.onclose = () => {
|
| 47 |
-
// clean up on disconnect
|
| 48 |
-
for (const set of rooms.values()) set.delete(socket);
|
| 49 |
-
};
|
| 50 |
-
|
| 51 |
-
return response;
|
| 52 |
}
|
| 53 |
|
| 54 |
-
//
|
| 55 |
if (url.pathname === "/") {
|
| 56 |
return new Response(`<!doctype html>
|
| 57 |
<html><head>
|
|
@@ -69,29 +33,20 @@ serve({
|
|
| 69 |
<div id="log"></div>
|
| 70 |
<input id="msg" placeholder="Type a message" style="width:80%;"/>
|
| 71 |
<button onclick="sendMsg()">Send</button>
|
| 72 |
-
|
| 73 |
<script>
|
| 74 |
var ws, currentRoom;
|
| 75 |
window.addEventListener('load', function() {
|
| 76 |
var scheme = location.protocol === 'https:' ? 'wss' : 'ws';
|
| 77 |
var socketUrl = scheme + '://' + location.host + '/';
|
| 78 |
-
console.log('Connecting to ' + socketUrl);
|
| 79 |
ws = new WebSocket(socketUrl);
|
| 80 |
-
|
| 81 |
ws.onopen = function(){ log('🔌 Connected'); };
|
| 82 |
ws.onmessage = function(ev){
|
| 83 |
var m=JSON.parse(ev.data);
|
| 84 |
log('['+m.roomId+'] '+m.message);
|
| 85 |
};
|
| 86 |
-
ws.onerror = function(e){
|
| 87 |
-
|
| 88 |
-
log('⚠️ WebSocket error');
|
| 89 |
-
};
|
| 90 |
-
ws.onclose = function(c){
|
| 91 |
-
log('❌ Disconnected (code='+c.code+')');
|
| 92 |
-
};
|
| 93 |
});
|
| 94 |
-
|
| 95 |
function joinRoom(){
|
| 96 |
var id = document.getElementById('room').value.trim();
|
| 97 |
if(!id) return alert('Enter room ID');
|
|
@@ -117,6 +72,44 @@ serve({
|
|
| 117 |
|
| 118 |
return new Response("Not Found", { status: 404 });
|
| 119 |
},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 120 |
});
|
| 121 |
|
| 122 |
-
console.log("✅ Bun realtime server running on port " + (
|
|
|
|
| 1 |
// server.js (Bun)
|
| 2 |
+
// import { serve } from "bun";
|
| 3 |
|
| 4 |
+
const rooms = new Map(); // roomId ⇒ Set<ServerWebSocket>
|
| 5 |
|
| 6 |
+
Bun.serve({
|
| 7 |
+
port: Number(Bun.env.PORT) || 7860,
|
| 8 |
|
| 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 and attach room data if needed
|
| 15 |
+
return server.upgrade(req) ? undefined : new Response("Upgrade failed", { status: 500 });
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
}
|
| 17 |
|
| 18 |
+
// Serve HTML UI for non-WebSocket requests
|
| 19 |
if (url.pathname === "/") {
|
| 20 |
return new Response(`<!doctype html>
|
| 21 |
<html><head>
|
|
|
|
| 33 |
<div id="log"></div>
|
| 34 |
<input id="msg" placeholder="Type a message" style="width:80%;"/>
|
| 35 |
<button onclick="sendMsg()">Send</button>
|
|
|
|
| 36 |
<script>
|
| 37 |
var ws, currentRoom;
|
| 38 |
window.addEventListener('load', function() {
|
| 39 |
var scheme = location.protocol === 'https:' ? 'wss' : 'ws';
|
| 40 |
var socketUrl = scheme + '://' + location.host + '/';
|
|
|
|
| 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(e){ log('⚠️ WebSocket error'); };
|
| 48 |
+
ws.onclose = function(c){ log('❌ Disconnected (code='+c.code+')'); };
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
});
|
|
|
|
| 50 |
function joinRoom(){
|
| 51 |
var id = document.getElementById('room').value.trim();
|
| 52 |
if(!id) return alert('Enter room ID');
|
|
|
|
| 72 |
|
| 73 |
return new Response("Not Found", { status: 404 });
|
| 74 |
},
|
| 75 |
+
|
| 76 |
+
websocket: {
|
| 77 |
+
message(ws, raw) {
|
| 78 |
+
let msg;
|
| 79 |
+
try { msg = JSON.parse(raw); } catch { return; }
|
| 80 |
+
|
| 81 |
+
if (msg.action === "join" && msg.roomId) {
|
| 82 |
+
// Leave all rooms
|
| 83 |
+
for (const set of rooms.values()) set.delete(ws);
|
| 84 |
+
// Join target room
|
| 85 |
+
let set = rooms.get(msg.roomId);
|
| 86 |
+
if (!set) {
|
| 87 |
+
set = new Set();
|
| 88 |
+
rooms.set(msg.roomId, set);
|
| 89 |
+
}
|
| 90 |
+
set.add(ws);
|
| 91 |
+
}
|
| 92 |
+
|
| 93 |
+
if (msg.action === "post" && msg.roomId && msg.message) {
|
| 94 |
+
const set = rooms.get(msg.roomId);
|
| 95 |
+
if (!set) return;
|
| 96 |
+
const payload = JSON.stringify({
|
| 97 |
+
roomId: msg.roomId,
|
| 98 |
+
message: msg.message,
|
| 99 |
+
timestamp: Date.now(),
|
| 100 |
+
});
|
| 101 |
+
for (const client of set) {
|
| 102 |
+
if (client.readyState === 1) { // 1 === OPEN
|
| 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);
|
| 111 |
+
}
|
| 112 |
+
}
|
| 113 |
});
|
| 114 |
|
| 115 |
+
console.log("✅ Bun realtime server running on port " + (Bun.env.PORT || 7860));
|