Spaces:
Sleeping
Sleeping
Update app.js
Browse files
app.js
CHANGED
|
@@ -1,162 +1,98 @@
|
|
| 1 |
-
// server.js
|
| 2 |
-
const path = require('path');
|
| 3 |
const express = require('express');
|
| 4 |
const http = require('http');
|
| 5 |
const WebSocket = require('ws');
|
| 6 |
-
const mediasoup = require('mediasoup');
|
| 7 |
|
| 8 |
const app = express();
|
| 9 |
-
const
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
ws.send(JSON.stringify({
|
| 51 |
-
action: 'producerTransportCreated',
|
| 52 |
-
data: {
|
| 53 |
-
id: transport.id,
|
| 54 |
-
iceParameters: transport.iceParameters,
|
| 55 |
-
iceCandidates: transport.iceCandidates,
|
| 56 |
-
dtlsParameters: transport.dtlsParameters
|
| 57 |
-
}
|
| 58 |
-
}));
|
| 59 |
-
break;
|
| 60 |
-
}
|
| 61 |
-
|
| 62 |
-
case 'connectProducerTransport': {
|
| 63 |
-
const { dtlsParameters } = data;
|
| 64 |
-
const { transport } = producerTransports.get(ws);
|
| 65 |
-
await transport.connect({ dtlsParameters });
|
| 66 |
-
ws.send(JSON.stringify({ action: 'producerTransportConnected' }));
|
| 67 |
-
break;
|
| 68 |
-
}
|
| 69 |
-
|
| 70 |
-
case 'produce': {
|
| 71 |
-
const { kind, rtpParameters } = data;
|
| 72 |
-
const { transport } = producerTransports.get(ws);
|
| 73 |
-
const producer = await transport.produce({ kind, rtpParameters });
|
| 74 |
-
producerTransports.get(ws).producer = producer;
|
| 75 |
-
ws.send(JSON.stringify({
|
| 76 |
-
action: 'produced',
|
| 77 |
-
data: { producerId: producer.id }
|
| 78 |
-
}));
|
| 79 |
-
break;
|
| 80 |
-
}
|
| 81 |
-
|
| 82 |
-
case 'createConsumerTransport': {
|
| 83 |
-
const transport = await router.createWebRtcTransport({
|
| 84 |
-
// listenIps: ['0.0.0.0'],
|
| 85 |
-
listenIps: ['127.0.0.1'],
|
| 86 |
-
enableUdp: true,
|
| 87 |
-
enableTcp: true
|
| 88 |
-
});
|
| 89 |
-
const arr = consumerTransports.get(ws) || [];
|
| 90 |
-
arr.push({ transport, consumer: null });
|
| 91 |
-
consumerTransports.set(ws, arr);
|
| 92 |
-
ws.send(JSON.stringify({
|
| 93 |
-
action: 'consumerTransportCreated',
|
| 94 |
-
data: {
|
| 95 |
-
id: transport.id,
|
| 96 |
-
iceParameters: transport.iceParameters,
|
| 97 |
-
iceCandidates: transport.iceCandidates,
|
| 98 |
-
dtlsParameters: transport.dtlsParameters
|
| 99 |
-
}
|
| 100 |
-
}));
|
| 101 |
-
break;
|
| 102 |
-
}
|
| 103 |
-
|
| 104 |
-
case 'connectConsumerTransport': {
|
| 105 |
-
const { dtlsParameters } = data;
|
| 106 |
-
const arr = consumerTransports.get(ws);
|
| 107 |
-
await arr[arr.length - 1].transport.connect({ dtlsParameters });
|
| 108 |
-
ws.send(JSON.stringify({ action: 'consumerTransportConnected' }));
|
| 109 |
-
break;
|
| 110 |
-
}
|
| 111 |
-
|
| 112 |
-
case 'consume': {
|
| 113 |
-
const producerEntry = [...producerTransports.values()].find(e => e.producer);
|
| 114 |
-
if (!producerEntry) {
|
| 115 |
-
ws.send(JSON.stringify({ action: 'consumeError', data: 'No producer available yet' }));
|
| 116 |
-
return;
|
| 117 |
-
}
|
| 118 |
-
const producer = producerEntry.producer;
|
| 119 |
-
const arr = consumerTransports.get(ws);
|
| 120 |
-
const { transport } = arr[arr.length - 1];
|
| 121 |
-
|
| 122 |
-
const consumer = await transport.consume({
|
| 123 |
-
producerId: producer.id,
|
| 124 |
-
rtpCapabilities: router.rtpCapabilities,
|
| 125 |
-
paused: false
|
| 126 |
-
});
|
| 127 |
-
arr[arr.length - 1].consumer = consumer;
|
| 128 |
-
|
| 129 |
-
ws.send(JSON.stringify({
|
| 130 |
-
action: 'consumed',
|
| 131 |
-
data: {
|
| 132 |
-
producerId: producer.id,
|
| 133 |
-
id: consumer.id,
|
| 134 |
-
kind: consumer.kind,
|
| 135 |
-
rtpParameters: consumer.rtpParameters
|
| 136 |
-
}
|
| 137 |
-
}));
|
| 138 |
-
break;
|
| 139 |
-
}
|
| 140 |
}
|
| 141 |
-
}
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 150 |
}
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 155 |
});
|
| 156 |
-
|
| 157 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 158 |
});
|
|
|
|
| 159 |
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
})();
|
|
|
|
|
|
|
|
|
|
| 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 |
+
|
| 11 |
+
app.get('/', (req, res) => {
|
| 12 |
+
res.send(`<!doctype html>
|
| 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 |
+
const roomId = document.getElementById('room').value;
|
| 30 |
+
ws = new WebSocket('ws://' + location.host);
|
| 31 |
+
ws.onopen = () => {
|
| 32 |
+
ws.send(JSON.stringify({ action: 'join', roomId }));
|
| 33 |
+
currentRoom = roomId;
|
| 34 |
+
log('Joined room: ' + roomId);
|
| 35 |
+
};
|
| 36 |
+
ws.onmessage = ({ data }) => {
|
| 37 |
+
const msg = JSON.parse(data);
|
| 38 |
+
log('[' + msg.roomId + '] ' + msg.message);
|
| 39 |
+
};
|
| 40 |
+
ws.onclose = () => log('Disconnected.');
|
| 41 |
+
}
|
| 42 |
+
|
| 43 |
+
function sendMsg() {
|
| 44 |
+
const txt = document.getElementById('msg').value;
|
| 45 |
+
if (ws && currentRoom) {
|
| 46 |
+
ws.send(JSON.stringify({ action: 'post', roomId: currentRoom, message: txt }));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
}
|
| 48 |
+
}
|
| 49 |
+
|
| 50 |
+
function log(t) {
|
| 51 |
+
document.getElementById('log').innerHTML += '<p>' + t + '</p>';
|
| 52 |
+
}
|
| 53 |
+
</script>
|
| 54 |
+
</body>
|
| 55 |
+
</html>`);
|
| 56 |
+
});
|
| 57 |
+
|
| 58 |
+
wss.on('connection', ws => {
|
| 59 |
+
let currentRoom = null;
|
| 60 |
+
|
| 61 |
+
ws.on('message', raw => {
|
| 62 |
+
let msg;
|
| 63 |
+
try { msg = JSON.parse(raw); } catch (e) { return; }
|
| 64 |
+
|
| 65 |
+
if (msg.action === 'join' && msg.roomId) {
|
| 66 |
+
if (currentRoom && rooms.has(currentRoom)) {
|
| 67 |
+
rooms.get(currentRoom).delete(ws);
|
| 68 |
}
|
| 69 |
+
currentRoom = msg.roomId;
|
| 70 |
+
if (!rooms.has(currentRoom)) rooms.set(currentRoom, new Set());
|
| 71 |
+
rooms.get(currentRoom).add(ws);
|
| 72 |
+
}
|
| 73 |
+
|
| 74 |
+
if (msg.action === 'post' && msg.roomId && msg.message) {
|
| 75 |
+
const clients = rooms.get(msg.roomId);
|
| 76 |
+
if (!clients) return;
|
| 77 |
+
const payload = JSON.stringify({
|
| 78 |
+
roomId: msg.roomId,
|
| 79 |
+
message: msg.message,
|
| 80 |
+
timestamp: Date.now()
|
| 81 |
});
|
| 82 |
+
for (const client of clients) {
|
| 83 |
+
if (client.readyState === WebSocket.OPEN) {
|
| 84 |
+
client.send(payload);
|
| 85 |
+
}
|
| 86 |
+
}
|
| 87 |
+
}
|
| 88 |
+
});
|
| 89 |
+
|
| 90 |
+
ws.on('close', () => {
|
| 91 |
+
if (currentRoom && rooms.has(currentRoom)) {
|
| 92 |
+
rooms.get(currentRoom).delete(ws);
|
| 93 |
+
}
|
| 94 |
});
|
| 95 |
+
});
|
| 96 |
|
| 97 |
+
const PORT = process.env.PORT || 7860;
|
| 98 |
+
server.listen(PORT, () => console.log(\`✅ Activity server running at http://localhost:\${PORT}/\`));
|
|
|