Spaces:
Sleeping
Sleeping
| const express = require('express'); | |
| const http = require('http'); | |
| const WebSocket = require('ws'); | |
| const app = express(); | |
| const server = http.createServer(app); | |
| const wss = new WebSocket.Server({ server }); | |
| const rooms = new Map(); | |
| app.get('/', (req, res) => { | |
| res.send(`<!doctype html> | |
| <html> | |
| <head> | |
| <title>Dubem Realtime Rooms</title> | |
| </head> | |
| <body style="font-family:sans-serif"> | |
| <h2>Join a Room & Send Messages</h2> | |
| <input id="room" placeholder="Room ID" /> | |
| <button onclick="joinRoom()">Join Room</button> | |
| <div id="log"></div> | |
| <input id="msg" placeholder="Type a message" /> | |
| <button onclick="sendMsg()">Send</button> | |
| <script> | |
| let ws, currentRoom = null; | |
| function joinRoom() { | |
| try { | |
| const roomId = document.getElementById('room').value; | |
| ws = new WebSocket('ws://' + location.host); | |
| ws.onopen = () => { | |
| ws.send(JSON.stringify({ action: 'join', roomId })); | |
| currentRoom = roomId; | |
| log('Joined room: ' + roomId); | |
| alert('Joined room: ' + roomId); | |
| }; | |
| ws.onmessage = ({ data }) => { | |
| const msg = JSON.parse(data); | |
| log('[' + msg.roomId + '] ' + msg.message); | |
| }; | |
| ws.onclose = () => log('Disconnected.'); | |
| } catch (err) { | |
| alert("err "+err) | |
| } | |
| } | |
| function sendMsg() { | |
| const txt = document.getElementById('msg').value; | |
| if (ws && currentRoom) { | |
| ws.send(JSON.stringify({ action: 'post', roomId: currentRoom, message: txt })); | |
| } | |
| } | |
| function log(t) { | |
| document.getElementById('log').innerHTML += '<p>' + t + '</p>'; | |
| } | |
| </script> | |
| </body> | |
| </html>`); | |
| }); | |
| wss.on('connection', ws => { | |
| let currentRoom = null; | |
| console.log("fresh connect") | |
| ws.on('message', raw => { | |
| let msg; | |
| console.log("message enter") | |
| try { msg = JSON.parse(raw); } catch (e) { return; } | |
| if (msg.action === 'join' && msg.roomId) { | |
| if (currentRoom && rooms.has(currentRoom)) { | |
| rooms.get(currentRoom).delete(ws); | |
| } | |
| currentRoom = msg.roomId; | |
| if (!rooms.has(currentRoom)) rooms.set(currentRoom, new Set()); | |
| rooms.get(currentRoom).add(ws); | |
| } | |
| if (msg.action === 'post' && msg.roomId && msg.message) { | |
| const clients = rooms.get(msg.roomId); | |
| if (!clients) return; | |
| const payload = JSON.stringify({ | |
| roomId: msg.roomId, | |
| message: msg.message, | |
| timestamp: Date.now() | |
| }); | |
| for (const client of clients) { | |
| if (client.readyState === WebSocket.OPEN) { | |
| client.send(payload); | |
| } | |
| } | |
| } | |
| console.log("message down") | |
| }); | |
| ws.on('close', () => { | |
| if (currentRoom && rooms.has(currentRoom)) { | |
| rooms.get(currentRoom).delete(ws); | |
| } | |
| console.log("room close") | |
| }); | |
| }); | |
| const PORT = process.env.PORT || 7860; | |
| server.listen(PORT, () => console.log(`✅ Activity server running at http://localhost:${PORT}`)); |