Pepguy commited on
Commit
d441744
·
verified ·
1 Parent(s): fde8995

Update app.js

Browse files
Files changed (1) hide show
  1. app.js +65 -38
app.js CHANGED
@@ -1,10 +1,11 @@
 
1
  const express = require('express');
2
  const http = require('http');
3
  const WebSocket = require('ws');
4
 
5
- const app = express();
6
  const server = http.createServer(app);
7
- const wss = new WebSocket.Server({ server });
8
 
9
  const rooms = new Map();
10
 
@@ -13,47 +14,76 @@ app.get('/', (req, res) => {
13
  <html>
14
  <head>
15
  <title>Dubem Realtime Rooms</title>
 
 
 
 
 
 
16
  </head>
17
- <body style="font-family:sans-serif">
18
  <h2>Join a Room & Send Messages</h2>
19
  <input id="room" placeholder="Room ID" />
20
  <button onclick="joinRoom()">Join Room</button>
21
  <div id="log"></div>
22
- <input id="msg" placeholder="Type a message" />
23
  <button onclick="sendMsg()">Send</button>
24
 
25
  <script>
26
- let ws, currentRoom = null;
 
 
 
 
 
27
 
28
- function joinRoom() {
29
- try {
30
- const roomId = document.getElementById('room').value;
31
- ws = new WebSocket('wss://' + location.host);
32
  ws.onopen = () => {
33
- ws.send(JSON.stringify({ action: 'join', roomId }));
34
- currentRoom = roomId;
35
- log('Joined room: ' + roomId);
36
- // alert('Joined room: ' + roomId);
37
  };
38
  ws.onmessage = ({ data }) => {
39
  const msg = JSON.parse(data);
40
- log('[' + msg.roomId + '] ' + msg.message);
 
 
 
 
 
 
41
  };
42
- ws.onclose = () => log('Disconnected.');
43
- } catch (err) {
44
- alert("err "+err)
 
 
 
 
45
  }
 
 
 
46
  }
47
 
48
  function sendMsg() {
49
- const txt = document.getElementById('msg').value;
50
- if (ws && currentRoom) {
51
- ws.send(JSON.stringify({ action: 'post', roomId: currentRoom, message: txt }));
 
52
  }
 
 
 
 
 
 
 
 
 
53
  }
54
 
55
  function log(t) {
56
- document.getElementById('log').innerHTML += '<p>' + t + '</p>';
 
 
57
  }
58
  </script>
59
  </body>
@@ -61,21 +91,21 @@ app.get('/', (req, res) => {
61
  });
62
 
63
  wss.on('connection', ws => {
64
- let currentRoom = null;
65
- console.log("fresh connect")
66
-
67
  ws.on('message', raw => {
68
  let msg;
69
- console.log("message enter")
70
- try { msg = JSON.parse(raw); } catch (e) { return; }
71
 
72
  if (msg.action === 'join' && msg.roomId) {
73
- if (currentRoom && rooms.has(currentRoom)) {
74
- rooms.get(currentRoom).delete(ws);
 
75
  }
76
- currentRoom = msg.roomId;
77
- if (!rooms.has(currentRoom)) rooms.set(currentRoom, new Set());
78
- rooms.get(currentRoom).add(ws);
 
 
79
  }
80
 
81
  if (msg.action === 'post' && msg.roomId && msg.message) {
@@ -92,18 +122,15 @@ wss.on('connection', ws => {
92
  }
93
  }
94
  }
95
- console.log("message down")
96
  });
97
 
98
  ws.on('close', () => {
99
- if (currentRoom && rooms.has(currentRoom)) {
100
- rooms.get(currentRoom).delete(ws);
101
-
102
  }
103
- currentRoom = null;
104
- console.log("room close")
105
  });
106
  });
107
 
108
  const PORT = process.env.PORT || 7860;
109
- server.listen(PORT, () => console.log(`✅ Activity server running at http://localhost:${PORT}`));
 
1
+ // server.js
2
  const express = require('express');
3
  const http = require('http');
4
  const WebSocket = require('ws');
5
 
6
+ const app = express();
7
  const server = http.createServer(app);
8
+ const wss = new WebSocket.Server({ server });
9
 
10
  const rooms = new Map();
11
 
 
14
  <html>
15
  <head>
16
  <title>Dubem Realtime Rooms</title>
17
+ <meta charset="utf-8" />
18
+ <style>
19
+ body { font-family: sans-serif; padding: 20px; background: #f0f0f0; }
20
+ #log { border: 1px solid #ccc; height: 200px; overflow-y: auto; padding: 10px; background: #fff; }
21
+ input, button { margin: 5px 0; padding: 8px; font-size: 16px; }
22
+ </style>
23
  </head>
24
+ <body>
25
  <h2>Join a Room & Send Messages</h2>
26
  <input id="room" placeholder="Room ID" />
27
  <button onclick="joinRoom()">Join Room</button>
28
  <div id="log"></div>
29
+ <input id="msg" placeholder="Type a message" style="width:80%;" />
30
  <button onclick="sendMsg()">Send</button>
31
 
32
  <script>
33
+ let ws;
34
+ let currentRoom = null;
35
+
36
+ window.addEventListener('load', () => {
37
+ const scheme = location.protocol === 'https:' ? 'wss' : 'ws';
38
+ ws = new WebSocket(\`\${scheme}://\${location.host}\`);
39
 
 
 
 
 
40
  ws.onopen = () => {
41
+ log('🔌 Connected to server');
 
 
 
42
  };
43
  ws.onmessage = ({ data }) => {
44
  const msg = JSON.parse(data);
45
+ log(\`[\${msg.roomId}] \${msg.message}\`);
46
+ };
47
+ ws.onclose = () => {
48
+ log('❌ Disconnected from server');
49
+ };
50
+ ws.onerror = err => {
51
+ log('⚠️ WebSocket error: ' + err.message);
52
  };
53
+ });
54
+
55
+ function joinRoom() {
56
+ const roomId = document.getElementById('room').value.trim();
57
+ if (!roomId) return alert('Please enter a room ID');
58
+ if (!ws || ws.readyState !== WebSocket.OPEN) {
59
+ return alert('Socket not open yet!');
60
  }
61
+ ws.send(JSON.stringify({ action: 'join', roomId }));
62
+ currentRoom = roomId;
63
+ log('➡️ Joined room: ' + roomId);
64
  }
65
 
66
  function sendMsg() {
67
+ const txt = document.getElementById('msg').value.trim();
68
+ if (!txt) return;
69
+ if (!ws || ws.readyState !== WebSocket.OPEN) {
70
+ return alert('Socket not open yet!');
71
  }
72
+ if (!currentRoom) {
73
+ return alert('Please join a room first');
74
+ }
75
+ ws.send(JSON.stringify({
76
+ action: 'post',
77
+ roomId: currentRoom,
78
+ message: txt
79
+ }));
80
+ document.getElementById('msg').value = '';
81
  }
82
 
83
  function log(t) {
84
+ const el = document.getElementById('log');
85
+ el.innerHTML += '<div>' + t + '</div>';
86
+ el.scrollTop = el.scrollHeight;
87
  }
88
  </script>
89
  </body>
 
91
  });
92
 
93
  wss.on('connection', ws => {
94
+ // When a client sends a 'join', we remove it from ALL rooms first
 
 
95
  ws.on('message', raw => {
96
  let msg;
97
+ try { msg = JSON.parse(raw); } catch { return; }
 
98
 
99
  if (msg.action === 'join' && msg.roomId) {
100
+ // leave all existing rooms
101
+ for (const clients of rooms.values()) {
102
+ clients.delete(ws);
103
  }
104
+ // join the new room
105
+ const room = msg.roomId;
106
+ if (!rooms.has(room)) rooms.set(room, new Set());
107
+ rooms.get(room).add(ws);
108
+ return;
109
  }
110
 
111
  if (msg.action === 'post' && msg.roomId && msg.message) {
 
122
  }
123
  }
124
  }
 
125
  });
126
 
127
  ws.on('close', () => {
128
+ // remove from all rooms on disconnect
129
+ for (const clients of rooms.values()) {
130
+ clients.delete(ws);
131
  }
 
 
132
  });
133
  });
134
 
135
  const PORT = process.env.PORT || 7860;
136
+ server.listen(PORT, () => console.log(\`✅ Activity server running at http://localhost:\${PORT}/\`));