Spaces:
Sleeping
Sleeping
Update app.js
Browse files
app.js
CHANGED
|
@@ -1,16 +1,17 @@
|
|
| 1 |
-
// server.js
|
| 2 |
-
|
| 3 |
-
const http = require('http');
|
| 4 |
-
const WebSocket = require('ws');
|
| 5 |
|
| 6 |
-
const
|
| 7 |
-
const server = http.createServer(app);
|
| 8 |
-
const wss = new WebSocket.Server({ server });
|
| 9 |
|
| 10 |
-
|
|
|
|
| 11 |
|
| 12 |
-
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
<html>
|
| 15 |
<head>
|
| 16 |
<title>Dubem Realtime Rooms</title>
|
|
@@ -22,7 +23,6 @@ app.get('/', (req, res) => {
|
|
| 22 |
</style>
|
| 23 |
</head>
|
| 24 |
<body>
|
| 25 |
-
<p>Trying</p>
|
| 26 |
<h2>Join a Room & Send Messages</h2>
|
| 27 |
<input id="room" placeholder="Room ID" />
|
| 28 |
<button onclick="joinRoom()">Join Room</button>
|
|
@@ -38,27 +38,18 @@ app.get('/', (req, res) => {
|
|
| 38 |
const scheme = location.protocol === 'https:' ? 'wss' : 'ws';
|
| 39 |
ws = new WebSocket(\`\${scheme}://\${location.host}\`);
|
| 40 |
|
| 41 |
-
ws.onopen = () =>
|
| 42 |
-
|
| 43 |
-
};
|
| 44 |
-
ws.onmessage = ({ data }) => {
|
| 45 |
const msg = JSON.parse(data);
|
| 46 |
log(\`[\${msg.roomId}] \${msg.message}\`);
|
| 47 |
};
|
| 48 |
-
ws.onclose
|
| 49 |
-
|
| 50 |
-
};
|
| 51 |
-
ws.onerror = err => {
|
| 52 |
-
log('⚠️ WebSocket error: ' + err.message);
|
| 53 |
-
};
|
| 54 |
});
|
| 55 |
|
| 56 |
function joinRoom() {
|
| 57 |
const roomId = document.getElementById('room').value.trim();
|
| 58 |
if (!roomId) return alert('Please enter a room ID');
|
| 59 |
-
if (!ws || ws.readyState !== WebSocket.OPEN) {
|
| 60 |
-
return alert('Socket not open yet!');
|
| 61 |
-
}
|
| 62 |
ws.send(JSON.stringify({ action: 'join', roomId }));
|
| 63 |
currentRoom = roomId;
|
| 64 |
log('➡️ Joined room: ' + roomId);
|
|
@@ -67,17 +58,8 @@ app.get('/', (req, res) => {
|
|
| 67 |
function sendMsg() {
|
| 68 |
const txt = document.getElementById('msg').value.trim();
|
| 69 |
if (!txt) return;
|
| 70 |
-
if (!
|
| 71 |
-
|
| 72 |
-
}
|
| 73 |
-
if (!currentRoom) {
|
| 74 |
-
return alert('Please join a room first');
|
| 75 |
-
}
|
| 76 |
-
ws.send(JSON.stringify({
|
| 77 |
-
action: 'post',
|
| 78 |
-
roomId: currentRoom,
|
| 79 |
-
message: txt
|
| 80 |
-
}));
|
| 81 |
document.getElementById('msg').value = '';
|
| 82 |
}
|
| 83 |
|
|
@@ -88,51 +70,60 @@ app.get('/', (req, res) => {
|
|
| 88 |
}
|
| 89 |
</script>
|
| 90 |
</body>
|
| 91 |
-
</html
|
| 92 |
-
}
|
|
|
|
|
|
|
| 93 |
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
|
|
|
|
|
|
| 104 |
}
|
| 105 |
-
// join the new room
|
| 106 |
-
const room = msg.roomId;
|
| 107 |
-
if (!rooms.has(room)) rooms.set(room, new Set());
|
| 108 |
-
rooms.get(room).add(ws);
|
| 109 |
-
return;
|
| 110 |
-
}
|
| 111 |
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
roomId: msg.roomId,
|
| 117 |
-
message: msg.message,
|
| 118 |
-
timestamp: Date.now()
|
| 119 |
-
});
|
| 120 |
-
for (const client of clients) {
|
| 121 |
-
if (client.readyState === WebSocket.OPEN) {
|
| 122 |
-
client.send(payload);
|
| 123 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 124 |
}
|
| 125 |
-
}
|
| 126 |
-
});
|
| 127 |
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 134 |
});
|
| 135 |
|
| 136 |
-
|
| 137 |
-
7860;
|
| 138 |
-
server.listen(PORT, () => console.log(`✅ Activity server running at http://localhost:${PORT}`));
|
|
|
|
| 1 |
+
// server.js (for Bun)
|
| 2 |
+
import { serve } from "bun";
|
|
|
|
|
|
|
| 3 |
|
| 4 |
+
const rooms = new Map(); // roomId → Set<WebSocket>
|
|
|
|
|
|
|
| 5 |
|
| 6 |
+
serve({
|
| 7 |
+
port: Number(process.env.PORT) || 7860,
|
| 8 |
|
| 9 |
+
// Serve the HTML page at “/”
|
| 10 |
+
async fetch(req) {
|
| 11 |
+
const url = new URL(req.url);
|
| 12 |
+
if (url.pathname !== "/") return new Response("Not Found", { status: 404 });
|
| 13 |
+
|
| 14 |
+
return new Response(`<!doctype html>
|
| 15 |
<html>
|
| 16 |
<head>
|
| 17 |
<title>Dubem Realtime Rooms</title>
|
|
|
|
| 23 |
</style>
|
| 24 |
</head>
|
| 25 |
<body>
|
|
|
|
| 26 |
<h2>Join a Room & Send Messages</h2>
|
| 27 |
<input id="room" placeholder="Room ID" />
|
| 28 |
<button onclick="joinRoom()">Join Room</button>
|
|
|
|
| 38 |
const scheme = location.protocol === 'https:' ? 'wss' : 'ws';
|
| 39 |
ws = new WebSocket(\`\${scheme}://\${location.host}\`);
|
| 40 |
|
| 41 |
+
ws.onopen = () => log('🔌 Connected to server');
|
| 42 |
+
ws.onmessage = ({data}) => {
|
|
|
|
|
|
|
| 43 |
const msg = JSON.parse(data);
|
| 44 |
log(\`[\${msg.roomId}] \${msg.message}\`);
|
| 45 |
};
|
| 46 |
+
ws.onclose = () => log('❌ Disconnected');
|
| 47 |
+
ws.onerror = err => log('⚠️ WebSocket error: ' + err.message);
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
});
|
| 49 |
|
| 50 |
function joinRoom() {
|
| 51 |
const roomId = document.getElementById('room').value.trim();
|
| 52 |
if (!roomId) return alert('Please enter a room ID');
|
|
|
|
|
|
|
|
|
|
| 53 |
ws.send(JSON.stringify({ action: 'join', roomId }));
|
| 54 |
currentRoom = roomId;
|
| 55 |
log('➡️ Joined room: ' + roomId);
|
|
|
|
| 58 |
function sendMsg() {
|
| 59 |
const txt = document.getElementById('msg').value.trim();
|
| 60 |
if (!txt) return;
|
| 61 |
+
if (!currentRoom) return alert('Join a room first');
|
| 62 |
+
ws.send(JSON.stringify({ action: 'post', roomId: currentRoom, message: txt }));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
document.getElementById('msg').value = '';
|
| 64 |
}
|
| 65 |
|
|
|
|
| 70 |
}
|
| 71 |
</script>
|
| 72 |
</body>
|
| 73 |
+
</html>`, {
|
| 74 |
+
headers: { "Content-Type": "text/html; charset=utf-8" },
|
| 75 |
+
});
|
| 76 |
+
},
|
| 77 |
|
| 78 |
+
// Hook into Bun's WebSocket handling
|
| 79 |
+
websocket: {
|
| 80 |
+
open(ws) {
|
| 81 |
+
// no-op here; clients join via “join” action
|
| 82 |
+
},
|
| 83 |
|
| 84 |
+
message(ws, raw) {
|
| 85 |
+
let msg;
|
| 86 |
+
try {
|
| 87 |
+
msg = JSON.parse(raw.toString());
|
| 88 |
+
} catch {
|
| 89 |
+
return;
|
| 90 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 91 |
|
| 92 |
+
if (msg.action === "join" && msg.roomId) {
|
| 93 |
+
// remove from all rooms
|
| 94 |
+
for (const clients of rooms.values()) {
|
| 95 |
+
clients.delete(ws);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 96 |
}
|
| 97 |
+
// add to new room
|
| 98 |
+
const roomSet = rooms.get(msg.roomId) || new Set();
|
| 99 |
+
roomSet.add(ws);
|
| 100 |
+
rooms.set(msg.roomId, roomSet);
|
| 101 |
+
return;
|
| 102 |
}
|
|
|
|
|
|
|
| 103 |
|
| 104 |
+
if (msg.action === "post" && msg.roomId && msg.message) {
|
| 105 |
+
const clients = rooms.get(msg.roomId);
|
| 106 |
+
if (!clients) return;
|
| 107 |
+
const payload = JSON.stringify({
|
| 108 |
+
roomId: msg.roomId,
|
| 109 |
+
message: msg.message,
|
| 110 |
+
timestamp: Date.now(),
|
| 111 |
+
});
|
| 112 |
+
for (const client of clients) {
|
| 113 |
+
if (client.readyState === 1) { // OPEN
|
| 114 |
+
client.send(payload);
|
| 115 |
+
}
|
| 116 |
+
}
|
| 117 |
+
}
|
| 118 |
+
},
|
| 119 |
+
|
| 120 |
+
close(ws) {
|
| 121 |
+
// on disconnect, remove from all rooms
|
| 122 |
+
for (const clients of rooms.values()) {
|
| 123 |
+
clients.delete(ws);
|
| 124 |
+
}
|
| 125 |
+
},
|
| 126 |
+
},
|
| 127 |
});
|
| 128 |
|
| 129 |
+
console.log("✅ Bun realtime server running on port " + (process.env.PORT || 7860));
|
|
|
|
|
|