Spaces:
Paused
Paused
Update server.js
Browse files
server.js
CHANGED
|
@@ -1,47 +1,15 @@
|
|
| 1 |
import express from "express";
|
| 2 |
-
import
|
| 3 |
-
import
|
| 4 |
-
import cors from "cors";
|
| 5 |
|
| 6 |
const app = express();
|
| 7 |
-
app.use(cors());
|
| 8 |
app.use(express.json());
|
| 9 |
|
| 10 |
-
console.log("π Starting
|
| 11 |
|
| 12 |
-
//
|
| 13 |
-
const
|
| 14 |
-
|
| 15 |
-
rtcMinPort: 10000,
|
| 16 |
-
rtcMaxPort: 10100,
|
| 17 |
-
});
|
| 18 |
-
|
| 19 |
-
console.log("β
mediasoup worker created [pid:%d]", worker.pid);
|
| 20 |
-
|
| 21 |
-
// Create router
|
| 22 |
-
const router = await worker.createRouter({
|
| 23 |
-
mediaCodecs: [
|
| 24 |
-
{
|
| 25 |
-
kind: "audio",
|
| 26 |
-
mimeType: "audio/opus",
|
| 27 |
-
clockRate: 48000,
|
| 28 |
-
channels: 2,
|
| 29 |
-
},
|
| 30 |
-
{
|
| 31 |
-
kind: "video",
|
| 32 |
-
mimeType: "video/VP8",
|
| 33 |
-
clockRate: 90000,
|
| 34 |
-
parameters: {},
|
| 35 |
-
},
|
| 36 |
-
],
|
| 37 |
-
});
|
| 38 |
-
|
| 39 |
-
console.log("β
mediasoup router created");
|
| 40 |
-
|
| 41 |
-
// Store transports and producers/consumers
|
| 42 |
-
const transports = new Map();
|
| 43 |
-
const producers = new Map();
|
| 44 |
-
const consumers = new Map();
|
| 45 |
|
| 46 |
// Middleware for logging
|
| 47 |
app.use((req, res, next) => {
|
|
@@ -57,7 +25,7 @@ app.get("/", (req, res) => {
|
|
| 57 |
<head>
|
| 58 |
<meta charset="UTF-8">
|
| 59 |
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
| 60 |
-
<title>
|
| 61 |
<style>
|
| 62 |
* { margin: 0; padding: 0; box-sizing: border-box; }
|
| 63 |
body {
|
|
@@ -110,20 +78,23 @@ app.get("/", (req, res) => {
|
|
| 110 |
</head>
|
| 111 |
<body>
|
| 112 |
<div class="container">
|
| 113 |
-
<h1>π‘ Stream Viewer</h1>
|
| 114 |
<div id="status" class="status waiting">Waiting for stream...</div>
|
| 115 |
-
<video id="video" autoplay playsinline controls></video>
|
| 116 |
<button id="playBtn" onclick="startViewing()">βΆοΈ Start Viewing</button>
|
| 117 |
<div style="margin-top: 20px; font-size: 14px; color: #666;">
|
| 118 |
<strong>Publisher:</strong> <a href="/publish" target="_blank">Click here to stream</a>
|
| 119 |
</div>
|
| 120 |
</div>
|
|
|
|
| 121 |
<script>
|
| 122 |
const video = document.getElementById('video');
|
| 123 |
const status = document.getElementById('status');
|
| 124 |
const playBtn = document.getElementById('playBtn');
|
| 125 |
-
let
|
| 126 |
-
let
|
|
|
|
|
|
|
| 127 |
|
| 128 |
async function startViewing() {
|
| 129 |
try {
|
|
@@ -131,102 +102,63 @@ app.get("/", (req, res) => {
|
|
| 131 |
status.textContent = 'Connecting...';
|
| 132 |
playBtn.disabled = true;
|
| 133 |
|
| 134 |
-
|
| 135 |
-
const
|
| 136 |
-
const rtpCapabilities = await rtpCapsResponse.json();
|
| 137 |
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
method: 'POST',
|
| 141 |
-
headers: { 'Content-Type': 'application/json' }
|
| 142 |
-
});
|
| 143 |
-
const transportData = await transportResponse.json();
|
| 144 |
-
consumeTransportId = transportData.id;
|
| 145 |
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
]
|
| 151 |
-
});
|
| 152 |
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 162 |
}
|
| 163 |
};
|
| 164 |
|
| 165 |
-
|
| 166 |
-
console.
|
|
|
|
|
|
|
| 167 |
};
|
| 168 |
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
console.log('πΉ Received track:', event.track.kind);
|
| 175 |
-
if (!video.srcObject) {
|
| 176 |
-
video.srcObject = event.streams[0];
|
| 177 |
-
}
|
| 178 |
-
status.className = 'status live';
|
| 179 |
-
status.textContent = 'π΄ LIVE';
|
| 180 |
};
|
| 181 |
|
| 182 |
-
// Create offer
|
| 183 |
-
const offer = await pc.createOffer();
|
| 184 |
-
await pc.setLocalDescription(offer);
|
| 185 |
-
|
| 186 |
-
// Wait for ICE gathering to complete
|
| 187 |
-
if (pc.iceGatheringState !== 'complete') {
|
| 188 |
-
await new Promise(resolve => {
|
| 189 |
-
const checkState = () => {
|
| 190 |
-
if (pc.iceGatheringState === 'complete') {
|
| 191 |
-
pc.removeEventListener('icegatheringstatechange', checkState);
|
| 192 |
-
resolve();
|
| 193 |
-
}
|
| 194 |
-
};
|
| 195 |
-
pc.addEventListener('icegatheringstatechange', checkState);
|
| 196 |
-
checkState();
|
| 197 |
-
});
|
| 198 |
-
}
|
| 199 |
-
|
| 200 |
-
console.log('β
ICE gathering complete');
|
| 201 |
-
|
| 202 |
-
// Connect transport with fixed DTLS parameters
|
| 203 |
-
await fetch('/api/connectConsumerTransport', {
|
| 204 |
-
method: 'POST',
|
| 205 |
-
headers: { 'Content-Type': 'application/json' },
|
| 206 |
-
body: JSON.stringify({
|
| 207 |
-
transportId: consumeTransportId,
|
| 208 |
-
dtlsParameters: extractDtlsParameters(pc.localDescription.sdp)
|
| 209 |
-
})
|
| 210 |
-
});
|
| 211 |
-
|
| 212 |
-
// Consume
|
| 213 |
-
const consumeResponse = await fetch('/api/consume', {
|
| 214 |
-
method: 'POST',
|
| 215 |
-
headers: { 'Content-Type': 'application/json' },
|
| 216 |
-
body: JSON.stringify({
|
| 217 |
-
transportId: consumeTransportId,
|
| 218 |
-
rtpCapabilities
|
| 219 |
-
})
|
| 220 |
-
});
|
| 221 |
-
|
| 222 |
-
if (!consumeResponse.ok) {
|
| 223 |
-
throw new Error('No stream available');
|
| 224 |
-
}
|
| 225 |
-
|
| 226 |
-
const { rtpParameters, id: consumerId } = await consumeResponse.json();
|
| 227 |
-
|
| 228 |
-
console.log('β
Viewing started');
|
| 229 |
-
|
| 230 |
} catch (error) {
|
| 231 |
console.error('Error:', error);
|
| 232 |
status.className = 'status error';
|
|
@@ -235,24 +167,35 @@ app.get("/", (req, res) => {
|
|
| 235 |
}
|
| 236 |
}
|
| 237 |
|
| 238 |
-
function
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
|
| 244 |
-
|
| 245 |
-
|
| 246 |
-
|
| 247 |
-
|
| 248 |
-
|
| 249 |
-
|
| 250 |
-
|
| 251 |
-
|
| 252 |
-
|
| 253 |
-
|
| 254 |
-
|
| 255 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 256 |
}
|
| 257 |
</script>
|
| 258 |
</body>
|
|
@@ -352,9 +295,8 @@ app.get("/publish", (req, res) => {
|
|
| 352 |
</div>
|
| 353 |
<script>
|
| 354 |
let stream = null;
|
| 355 |
-
let
|
| 356 |
-
let
|
| 357 |
-
let audioProducerId = null;
|
| 358 |
const status = document.getElementById('status');
|
| 359 |
const startBtn = document.getElementById('startBtn');
|
| 360 |
const stopBtn = document.getElementById('stopBtn');
|
|
@@ -382,102 +324,52 @@ app.get("/publish", (req, res) => {
|
|
| 382 |
document.getElementById('preview').srcObject = stream;
|
| 383 |
status.textContent = 'Connecting to server...';
|
| 384 |
|
| 385 |
-
//
|
| 386 |
-
const
|
| 387 |
-
const
|
| 388 |
-
|
| 389 |
-
// Create producer transport
|
| 390 |
-
const transportResponse = await fetch('/api/createProducerTransport', {
|
| 391 |
-
method: 'POST',
|
| 392 |
-
headers: { 'Content-Type': 'application/json' }
|
| 393 |
-
});
|
| 394 |
-
const transportData = await transportResponse.json();
|
| 395 |
-
produceTransportId = transportData.id;
|
| 396 |
-
|
| 397 |
-
// Create local peer connection to get parameters
|
| 398 |
-
const pc = new RTCPeerConnection();
|
| 399 |
-
stream.getTracks().forEach(track => pc.addTrack(track, stream));
|
| 400 |
|
| 401 |
-
|
| 402 |
-
|
| 403 |
|
| 404 |
-
|
| 405 |
-
|
| 406 |
-
|
| 407 |
-
|
| 408 |
-
|
| 409 |
-
|
| 410 |
-
headers: { 'Content-Type': 'application/json' },
|
| 411 |
-
body: JSON.stringify({
|
| 412 |
-
transportId: produceTransportId,
|
| 413 |
-
dtlsParameters
|
| 414 |
-
})
|
| 415 |
-
});
|
| 416 |
-
|
| 417 |
-
// Produce video
|
| 418 |
-
const videoTrack = stream.getVideoTracks()[0];
|
| 419 |
-
if (videoTrack) {
|
| 420 |
-
const videoParams = extractTrackParameters(pc.localDescription.sdp, 'video');
|
| 421 |
|
| 422 |
-
//
|
| 423 |
-
const
|
| 424 |
-
|
| 425 |
-
|
| 426 |
-
|
| 427 |
-
}
|
| 428 |
|
| 429 |
-
|
| 430 |
|
| 431 |
-
|
| 432 |
-
|
| 433 |
-
|
| 434 |
-
|
| 435 |
-
|
| 436 |
-
kind: 'video',
|
| 437 |
-
rtpParameters: videoParams
|
| 438 |
-
})
|
| 439 |
-
});
|
| 440 |
|
| 441 |
-
|
| 442 |
-
const error = await videoResponse.json();
|
| 443 |
-
throw new Error('Video producer failed: ' + error.error);
|
| 444 |
-
}
|
| 445 |
|
| 446 |
-
|
| 447 |
-
|
| 448 |
-
|
| 449 |
-
}
|
| 450 |
|
| 451 |
-
|
| 452 |
-
|
| 453 |
-
|
| 454 |
-
|
| 455 |
-
|
| 456 |
-
|
| 457 |
-
|
| 458 |
-
const audioResponse = await fetch('/api/produce', {
|
| 459 |
-
method: 'POST',
|
| 460 |
-
headers: { 'Content-Type': 'application/json' },
|
| 461 |
-
body: JSON.stringify({
|
| 462 |
-
transportId: produceTransportId,
|
| 463 |
-
kind: 'audio',
|
| 464 |
-
rtpParameters: audioParams
|
| 465 |
-
})
|
| 466 |
-
});
|
| 467 |
-
|
| 468 |
-
if (!audioResponse.ok) {
|
| 469 |
-
const error = await audioResponse.json();
|
| 470 |
-
throw new Error('Audio producer failed: ' + error.error);
|
| 471 |
-
}
|
| 472 |
-
|
| 473 |
-
const { id } = await audioResponse.json();
|
| 474 |
-
audioProducerId = id;
|
| 475 |
-
console.log('β
Audio producer created:', id);
|
| 476 |
-
}
|
| 477 |
|
| 478 |
-
|
| 479 |
-
|
| 480 |
-
|
| 481 |
|
| 482 |
} catch (error) {
|
| 483 |
console.error('Error:', error);
|
|
@@ -489,7 +381,15 @@ app.get("/publish", (req, res) => {
|
|
| 489 |
}
|
| 490 |
|
| 491 |
function stopStream() {
|
| 492 |
-
if (
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 493 |
stream = null;
|
| 494 |
document.getElementById('preview').srcObject = null;
|
| 495 |
status.className = 'status idle';
|
|
@@ -497,308 +397,116 @@ app.get("/publish", (req, res) => {
|
|
| 497 |
startBtn.disabled = false;
|
| 498 |
stopBtn.disabled = true;
|
| 499 |
}
|
| 500 |
-
|
| 501 |
-
function extractDtlsParameters(sdp) {
|
| 502 |
-
const fingerprintLine = sdp.match(/a=fingerprint:(\\S+) (\\S+)/);
|
| 503 |
-
const setupLine = sdp.match(/a=setup:(\\S+)/);
|
| 504 |
-
let role = 'auto';
|
| 505 |
-
if (setupLine) {
|
| 506 |
-
const setup = setupLine[1];
|
| 507 |
-
// mediasoup only accepts 'client', 'server', or 'auto'
|
| 508 |
-
if (setup === 'active') role = 'client';
|
| 509 |
-
else if (setup === 'passive') role = 'server';
|
| 510 |
-
else if (setup === 'actpass') role = 'auto';
|
| 511 |
-
}
|
| 512 |
-
return {
|
| 513 |
-
role: role,
|
| 514 |
-
fingerprints: [{
|
| 515 |
-
algorithm: fingerprintLine[1],
|
| 516 |
-
value: fingerprintLine[2]
|
| 517 |
-
}]
|
| 518 |
-
};
|
| 519 |
-
}
|
| 520 |
-
|
| 521 |
-
function extractTrackParameters(sdp, kind) {
|
| 522 |
-
// Parse SDP to get proper RTP parameters
|
| 523 |
-
const lines = sdp.split('\\r\\n');
|
| 524 |
-
const mediaSection = [];
|
| 525 |
-
let inSection = false;
|
| 526 |
-
|
| 527 |
-
for (const line of lines) {
|
| 528 |
-
if (line.startsWith('m=' + kind)) {
|
| 529 |
-
inSection = true;
|
| 530 |
-
} else if (line.startsWith('m=') && inSection) {
|
| 531 |
-
break;
|
| 532 |
-
}
|
| 533 |
-
if (inSection) {
|
| 534 |
-
mediaSection.push(line);
|
| 535 |
-
}
|
| 536 |
-
}
|
| 537 |
-
|
| 538 |
-
// Extract codec info
|
| 539 |
-
const rtpmapRegex = /a=rtpmap:(\\d+) ([^\\/\\s]+)\\/(\\d+)(?:\\/(\\d+))?/;
|
| 540 |
-
const fmtpRegex = /a=fmtp:(\\d+) (.+)/;
|
| 541 |
-
|
| 542 |
-
const codecs = [];
|
| 543 |
-
const payloadTypes = new Set();
|
| 544 |
-
|
| 545 |
-
for (const line of mediaSection) {
|
| 546 |
-
const rtpmapMatch = line.match(rtpmapRegex);
|
| 547 |
-
if (rtpmapMatch) {
|
| 548 |
-
const payloadType = parseInt(rtpmapMatch[1]);
|
| 549 |
-
const codecName = rtpmapMatch[2];
|
| 550 |
-
const clockRate = parseInt(rtpmapMatch[3]);
|
| 551 |
-
const channels = rtpmapMatch[4] ? parseInt(rtpmapMatch[4]) : undefined;
|
| 552 |
-
|
| 553 |
-
// Only include codecs that match our kind
|
| 554 |
-
if ((kind === 'video' && (codecName === 'VP8' || codecName === 'VP9' || codecName === 'H264')) ||
|
| 555 |
-
(kind === 'audio' && codecName.toLowerCase() === 'opus')) {
|
| 556 |
-
|
| 557 |
-
const codec = {
|
| 558 |
-
mimeType: \`\${kind}/\${codecName}\`,
|
| 559 |
-
clockRate: clockRate,
|
| 560 |
-
payloadType: payloadType
|
| 561 |
-
};
|
| 562 |
-
|
| 563 |
-
if (channels) {
|
| 564 |
-
codec.channels = channels;
|
| 565 |
-
}
|
| 566 |
-
|
| 567 |
-
// Look for matching fmtp line
|
| 568 |
-
for (const fmtpLine of mediaSection) {
|
| 569 |
-
const fmtpMatch = fmtpLine.match(fmtpRegex);
|
| 570 |
-
if (fmtpMatch && parseInt(fmtpMatch[1]) === payloadType) {
|
| 571 |
-
codec.parameters = {};
|
| 572 |
-
const params = fmtpMatch[2].split(';');
|
| 573 |
-
for (const param of params) {
|
| 574 |
-
const parts = param.trim().split('=');
|
| 575 |
-
if (parts.length === 2) {
|
| 576 |
-
const key = parts[0].trim();
|
| 577 |
-
const value = parts[1].trim();
|
| 578 |
-
// Convert numeric values
|
| 579 |
-
codec.parameters[key] = isNaN(value) ? value : parseInt(value);
|
| 580 |
-
}
|
| 581 |
-
}
|
| 582 |
-
}
|
| 583 |
-
}
|
| 584 |
-
|
| 585 |
-
codecs.push(codec);
|
| 586 |
-
payloadTypes.add(payloadType);
|
| 587 |
-
break; // Use first matching codec
|
| 588 |
-
}
|
| 589 |
-
}
|
| 590 |
-
}
|
| 591 |
-
|
| 592 |
-
if (codecs.length === 0) {
|
| 593 |
-
throw new Error(\`No compatible \${kind} codec found\`);
|
| 594 |
-
}
|
| 595 |
-
|
| 596 |
-
// Extract SSRC
|
| 597 |
-
const ssrcMatch = mediaSection.find(l => l.startsWith('a=ssrc:'));
|
| 598 |
-
const ssrc = ssrcMatch ? parseInt(ssrcMatch.split(':')[1].split(' ')[0]) : Math.floor(Math.random() * 1000000000);
|
| 599 |
-
|
| 600 |
-
return {
|
| 601 |
-
codecs: codecs,
|
| 602 |
-
encodings: [{ ssrc: ssrc }]
|
| 603 |
-
};
|
| 604 |
-
}
|
| 605 |
</script>
|
| 606 |
</body>
|
| 607 |
</html>
|
| 608 |
`);
|
| 609 |
});
|
| 610 |
|
| 611 |
-
//
|
| 612 |
-
app.get("/
|
| 613 |
-
|
| 614 |
-
|
| 615 |
-
|
| 616 |
-
|
| 617 |
-
|
| 618 |
-
try {
|
| 619 |
-
console.log("π₯ Creating producer transport");
|
| 620 |
-
|
| 621 |
-
// Get the host from request headers for announced IP
|
| 622 |
-
const host = req.headers.host?.split(':')[0] || req.hostname;
|
| 623 |
-
|
| 624 |
-
const transport = await router.createWebRtcTransport({
|
| 625 |
-
listenIps: [
|
| 626 |
-
{
|
| 627 |
-
ip: "0.0.0.0",
|
| 628 |
-
announcedIp: host // Use the actual domain/IP that clients connect to
|
| 629 |
-
}
|
| 630 |
-
],
|
| 631 |
-
enableUdp: true,
|
| 632 |
-
enableTcp: true,
|
| 633 |
-
preferUdp: true,
|
| 634 |
-
initialAvailableOutgoingBitrate: 1000000,
|
| 635 |
-
});
|
| 636 |
-
|
| 637 |
-
transports.set(transport.id, transport);
|
| 638 |
-
|
| 639 |
-
res.json({
|
| 640 |
-
id: transport.id,
|
| 641 |
-
iceParameters: transport.iceParameters,
|
| 642 |
-
iceCandidates: transport.iceCandidates,
|
| 643 |
-
dtlsParameters: transport.dtlsParameters,
|
| 644 |
-
});
|
| 645 |
-
|
| 646 |
-
console.log("β
Producer transport created:", transport.id);
|
| 647 |
-
} catch (error) {
|
| 648 |
-
console.error("β Error creating producer transport:", error);
|
| 649 |
-
res.status(500).json({ error: error.message });
|
| 650 |
-
}
|
| 651 |
-
});
|
| 652 |
-
|
| 653 |
-
app.post("/api/createConsumerTransport", async (req, res) => {
|
| 654 |
-
try {
|
| 655 |
-
console.log("ποΈ Creating consumer transport");
|
| 656 |
-
|
| 657 |
-
// Get the host from request headers for announced IP
|
| 658 |
-
const host = req.headers.host?.split(':')[0] || req.hostname;
|
| 659 |
-
|
| 660 |
-
const transport = await router.createWebRtcTransport({
|
| 661 |
-
listenIps: [
|
| 662 |
-
{
|
| 663 |
-
ip: "0.0.0.0",
|
| 664 |
-
announcedIp: host // Use the actual domain/IP that clients connect to
|
| 665 |
-
}
|
| 666 |
-
],
|
| 667 |
-
enableUdp: true,
|
| 668 |
-
enableTcp: true,
|
| 669 |
-
preferUdp: true,
|
| 670 |
-
initialAvailableOutgoingBitrate: 1000000,
|
| 671 |
-
});
|
| 672 |
-
|
| 673 |
-
transports.set(transport.id, transport);
|
| 674 |
-
|
| 675 |
-
res.json({
|
| 676 |
-
id: transport.id,
|
| 677 |
-
iceParameters: transport.iceParameters,
|
| 678 |
-
iceCandidates: transport.iceCandidates,
|
| 679 |
-
dtlsParameters: transport.dtlsParameters,
|
| 680 |
-
});
|
| 681 |
-
|
| 682 |
-
console.log("β
Consumer transport created:", transport.id);
|
| 683 |
-
} catch (error) {
|
| 684 |
-
console.error("β Error creating consumer transport:", error);
|
| 685 |
-
res.status(500).json({ error: error.message });
|
| 686 |
-
}
|
| 687 |
-
});
|
| 688 |
-
|
| 689 |
-
app.post("/api/connectProducerTransport", async (req, res) => {
|
| 690 |
-
try {
|
| 691 |
-
const { transportId, dtlsParameters } = req.body;
|
| 692 |
-
const transport = transports.get(transportId);
|
| 693 |
-
|
| 694 |
-
if (!transport) {
|
| 695 |
-
return res.status(404).json({ error: "Transport not found" });
|
| 696 |
-
}
|
| 697 |
-
|
| 698 |
-
await transport.connect({ dtlsParameters });
|
| 699 |
-
console.log("β
Producer transport connected:", transportId);
|
| 700 |
-
res.json({ success: true });
|
| 701 |
-
} catch (error) {
|
| 702 |
-
console.error("β Error connecting producer transport:", error);
|
| 703 |
-
res.status(500).json({ error: error.message });
|
| 704 |
-
}
|
| 705 |
});
|
| 706 |
|
| 707 |
-
|
| 708 |
-
|
| 709 |
-
const { transportId, dtlsParameters } = req.body;
|
| 710 |
-
const transport = transports.get(transportId);
|
| 711 |
-
|
| 712 |
-
if (!transport) {
|
| 713 |
-
return res.status(404).json({ error: "Transport not found" });
|
| 714 |
-
}
|
| 715 |
-
|
| 716 |
-
await transport.connect({ dtlsParameters });
|
| 717 |
-
console.log("β
Consumer transport connected:", transportId);
|
| 718 |
-
res.json({ success: true });
|
| 719 |
-
} catch (error) {
|
| 720 |
-
console.error("β Error connecting consumer transport:", error);
|
| 721 |
-
res.status(500).json({ error: error.message });
|
| 722 |
-
}
|
| 723 |
-
});
|
| 724 |
|
| 725 |
-
|
| 726 |
-
|
| 727 |
-
|
| 728 |
-
|
| 729 |
-
|
| 730 |
-
if (!transport) {
|
| 731 |
-
return res.status(404).json({ error: "Transport not found" });
|
| 732 |
-
}
|
| 733 |
-
|
| 734 |
-
// Log the incoming parameters for debugging
|
| 735 |
-
console.log(`π Attempting to create ${kind} producer with parameters:`, JSON.stringify(rtpParameters, null, 2));
|
| 736 |
-
|
| 737 |
-
const producer = await transport.produce({ kind, rtpParameters });
|
| 738 |
-
producers.set(producer.id, producer);
|
| 739 |
-
|
| 740 |
-
console.log(`β
${kind} producer created:`, producer.id);
|
| 741 |
-
res.json({ id: producer.id });
|
| 742 |
-
} catch (error) {
|
| 743 |
-
console.error("β Error creating producer:", error);
|
| 744 |
-
res.status(500).json({ error: error.message });
|
| 745 |
-
}
|
| 746 |
});
|
| 747 |
|
| 748 |
-
|
| 749 |
-
|
| 750 |
-
|
| 751 |
-
|
| 752 |
-
|
| 753 |
-
|
| 754 |
-
|
| 755 |
-
|
| 756 |
-
|
| 757 |
-
|
| 758 |
-
|
| 759 |
-
|
| 760 |
-
|
| 761 |
-
|
| 762 |
-
|
| 763 |
-
|
| 764 |
-
|
| 765 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 766 |
}
|
| 767 |
-
|
| 768 |
-
|
| 769 |
-
|
| 770 |
-
|
| 771 |
-
paused: false,
|
| 772 |
-
});
|
| 773 |
-
|
| 774 |
-
consumers.set(consumer.id, consumer);
|
| 775 |
-
|
| 776 |
-
console.log("β
Consumer created:", consumer.id);
|
| 777 |
-
res.json({
|
| 778 |
-
id: consumer.id,
|
| 779 |
-
producerId: producer.id,
|
| 780 |
-
kind: consumer.kind,
|
| 781 |
-
rtpParameters: consumer.rtpParameters,
|
| 782 |
-
});
|
| 783 |
-
} catch (error) {
|
| 784 |
-
console.error("β Error creating consumer:", error);
|
| 785 |
-
res.status(500).json({ error: error.message });
|
| 786 |
-
}
|
| 787 |
-
});
|
| 788 |
-
|
| 789 |
-
app.get("/status", (req, res) => {
|
| 790 |
-
res.json({
|
| 791 |
-
hasStream: producers.size > 0,
|
| 792 |
-
workerPid: worker.pid,
|
| 793 |
-
producers: producers.size,
|
| 794 |
-
consumers: consumers.size,
|
| 795 |
-
transports: transports.size,
|
| 796 |
});
|
| 797 |
});
|
| 798 |
|
| 799 |
// Start server
|
| 800 |
const PORT = process.env.PORT || 7860;
|
| 801 |
-
|
| 802 |
console.log(`\n${"=".repeat(50)}`);
|
| 803 |
console.log(`π Server running on port ${PORT}`);
|
| 804 |
console.log(`${"=".repeat(50)}`);
|
|
|
|
| 1 |
import express from "express";
|
| 2 |
+
import { createServer } from "http";
|
| 3 |
+
import { WebSocketServer } from "ws";
|
|
|
|
| 4 |
|
| 5 |
const app = express();
|
|
|
|
| 6 |
app.use(express.json());
|
| 7 |
|
| 8 |
+
console.log("π Starting WebSocket streaming server...");
|
| 9 |
|
| 10 |
+
// Store active streams
|
| 11 |
+
const streams = new Map(); // streamId -> { publisher: ws, viewers: Set<ws>, metadata }
|
| 12 |
+
let currentStream = null;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
// Middleware for logging
|
| 15 |
app.use((req, res, next) => {
|
|
|
|
| 25 |
<head>
|
| 26 |
<meta charset="UTF-8">
|
| 27 |
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
| 28 |
+
<title>Live Stream Viewer</title>
|
| 29 |
<style>
|
| 30 |
* { margin: 0; padding: 0; box-sizing: border-box; }
|
| 31 |
body {
|
|
|
|
| 78 |
</head>
|
| 79 |
<body>
|
| 80 |
<div class="container">
|
| 81 |
+
<h1>π‘ Live Stream Viewer</h1>
|
| 82 |
<div id="status" class="status waiting">Waiting for stream...</div>
|
| 83 |
+
<video id="video" autoplay playsinline controls muted></video>
|
| 84 |
<button id="playBtn" onclick="startViewing()">βΆοΈ Start Viewing</button>
|
| 85 |
<div style="margin-top: 20px; font-size: 14px; color: #666;">
|
| 86 |
<strong>Publisher:</strong> <a href="/publish" target="_blank">Click here to stream</a>
|
| 87 |
</div>
|
| 88 |
</div>
|
| 89 |
+
<script src="https://cdn.jsdelivr.net/npm/hls.js@latest"></script>
|
| 90 |
<script>
|
| 91 |
const video = document.getElementById('video');
|
| 92 |
const status = document.getElementById('status');
|
| 93 |
const playBtn = document.getElementById('playBtn');
|
| 94 |
+
let ws = null;
|
| 95 |
+
let mediaSource = null;
|
| 96 |
+
let sourceBuffer = null;
|
| 97 |
+
let queue = [];
|
| 98 |
|
| 99 |
async function startViewing() {
|
| 100 |
try {
|
|
|
|
| 102 |
status.textContent = 'Connecting...';
|
| 103 |
playBtn.disabled = true;
|
| 104 |
|
| 105 |
+
const protocol = window.location.protocol === 'https:' ? 'wss:' : 'ws:';
|
| 106 |
+
const wsUrl = \`\${protocol}//\${window.location.host}/stream\`;
|
|
|
|
| 107 |
|
| 108 |
+
ws = new WebSocket(wsUrl);
|
| 109 |
+
ws.binaryType = 'arraybuffer';
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 110 |
|
| 111 |
+
ws.onopen = () => {
|
| 112 |
+
console.log('β
WebSocket connected');
|
| 113 |
+
ws.send(JSON.stringify({ type: 'viewer' }));
|
| 114 |
+
};
|
|
|
|
|
|
|
| 115 |
|
| 116 |
+
ws.onmessage = async (event) => {
|
| 117 |
+
if (typeof event.data === 'string') {
|
| 118 |
+
const msg = JSON.parse(event.data);
|
| 119 |
+
|
| 120 |
+
if (msg.type === 'stream_available') {
|
| 121 |
+
status.className = 'status live';
|
| 122 |
+
status.textContent = 'π΄ LIVE';
|
| 123 |
+
} else if (msg.type === 'no_stream') {
|
| 124 |
+
status.className = 'status waiting';
|
| 125 |
+
status.textContent = 'Waiting for stream...';
|
| 126 |
+
} else if (msg.type === 'init') {
|
| 127 |
+
// Initialize MediaSource with codec info
|
| 128 |
+
if ('MediaSource' in window) {
|
| 129 |
+
setupMediaSource(msg.codec);
|
| 130 |
+
} else {
|
| 131 |
+
// Fallback to simple blob URL for older browsers
|
| 132 |
+
setupBlobPlayback();
|
| 133 |
+
}
|
| 134 |
+
}
|
| 135 |
+
} else {
|
| 136 |
+
// Binary video data
|
| 137 |
+
if (sourceBuffer && !sourceBuffer.updating) {
|
| 138 |
+
try {
|
| 139 |
+
sourceBuffer.appendBuffer(event.data);
|
| 140 |
+
} catch (e) {
|
| 141 |
+
queue.push(event.data);
|
| 142 |
+
}
|
| 143 |
+
} else {
|
| 144 |
+
queue.push(event.data);
|
| 145 |
+
}
|
| 146 |
}
|
| 147 |
};
|
| 148 |
|
| 149 |
+
ws.onerror = (error) => {
|
| 150 |
+
console.error('WebSocket error:', error);
|
| 151 |
+
status.className = 'status error';
|
| 152 |
+
status.textContent = 'β Connection error';
|
| 153 |
};
|
| 154 |
|
| 155 |
+
ws.onclose = () => {
|
| 156 |
+
console.log('WebSocket closed');
|
| 157 |
+
status.className = 'status waiting';
|
| 158 |
+
status.textContent = 'Disconnected';
|
| 159 |
+
playBtn.disabled = false;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 160 |
};
|
| 161 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 162 |
} catch (error) {
|
| 163 |
console.error('Error:', error);
|
| 164 |
status.className = 'status error';
|
|
|
|
| 167 |
}
|
| 168 |
}
|
| 169 |
|
| 170 |
+
function setupMediaSource(codec) {
|
| 171 |
+
mediaSource = new MediaSource();
|
| 172 |
+
video.src = URL.createObjectURL(mediaSource);
|
| 173 |
+
|
| 174 |
+
mediaSource.addEventListener('sourceopen', () => {
|
| 175 |
+
console.log('MediaSource opened');
|
| 176 |
+
try {
|
| 177 |
+
sourceBuffer = mediaSource.addSourceBuffer(codec || 'video/webm; codecs="vp8,opus"');
|
| 178 |
+
|
| 179 |
+
sourceBuffer.addEventListener('updateend', () => {
|
| 180 |
+
if (queue.length > 0 && !sourceBuffer.updating) {
|
| 181 |
+
const data = queue.shift();
|
| 182 |
+
try {
|
| 183 |
+
sourceBuffer.appendBuffer(data);
|
| 184 |
+
} catch (e) {
|
| 185 |
+
console.error('Error appending buffer:', e);
|
| 186 |
+
}
|
| 187 |
+
}
|
| 188 |
+
});
|
| 189 |
+
} catch (e) {
|
| 190 |
+
console.error('Error creating source buffer:', e);
|
| 191 |
+
setupBlobPlayback();
|
| 192 |
+
}
|
| 193 |
+
});
|
| 194 |
+
}
|
| 195 |
+
|
| 196 |
+
function setupBlobPlayback() {
|
| 197 |
+
console.log('Using blob playback fallback');
|
| 198 |
+
// Simple fallback - just update video source periodically
|
| 199 |
}
|
| 200 |
</script>
|
| 201 |
</body>
|
|
|
|
| 295 |
</div>
|
| 296 |
<script>
|
| 297 |
let stream = null;
|
| 298 |
+
let ws = null;
|
| 299 |
+
let mediaRecorder = null;
|
|
|
|
| 300 |
const status = document.getElementById('status');
|
| 301 |
const startBtn = document.getElementById('startBtn');
|
| 302 |
const stopBtn = document.getElementById('stopBtn');
|
|
|
|
| 324 |
document.getElementById('preview').srcObject = stream;
|
| 325 |
status.textContent = 'Connecting to server...';
|
| 326 |
|
| 327 |
+
// Connect WebSocket
|
| 328 |
+
const protocol = window.location.protocol === 'https:' ? 'wss:' : 'ws:';
|
| 329 |
+
const wsUrl = \`\${protocol}//\${window.location.host}/stream\`;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 330 |
|
| 331 |
+
ws = new WebSocket(wsUrl);
|
| 332 |
+
ws.binaryType = 'arraybuffer';
|
| 333 |
|
| 334 |
+
ws.onopen = () => {
|
| 335 |
+
console.log('β
WebSocket connected');
|
| 336 |
+
ws.send(JSON.stringify({
|
| 337 |
+
type: 'publisher',
|
| 338 |
+
codec: 'video/webm; codecs="vp8,opus"'
|
| 339 |
+
}));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 340 |
|
| 341 |
+
// Start recording and streaming
|
| 342 |
+
const options = {
|
| 343 |
+
mimeType: 'video/webm;codecs=vp8,opus',
|
| 344 |
+
videoBitsPerSecond: 2500000
|
| 345 |
+
};
|
|
|
|
| 346 |
|
| 347 |
+
mediaRecorder = new MediaRecorder(stream, options);
|
| 348 |
|
| 349 |
+
mediaRecorder.ondataavailable = (event) => {
|
| 350 |
+
if (event.data.size > 0 && ws.readyState === WebSocket.OPEN) {
|
| 351 |
+
ws.send(event.data);
|
| 352 |
+
}
|
| 353 |
+
};
|
|
|
|
|
|
|
|
|
|
|
|
|
| 354 |
|
| 355 |
+
mediaRecorder.start(100); // Send chunks every 100ms
|
|
|
|
|
|
|
|
|
|
| 356 |
|
| 357 |
+
status.className = 'status streaming';
|
| 358 |
+
status.textContent = 'π΄ STREAMING LIVE';
|
| 359 |
+
stopBtn.disabled = false;
|
| 360 |
+
};
|
| 361 |
|
| 362 |
+
ws.onerror = (error) => {
|
| 363 |
+
console.error('WebSocket error:', error);
|
| 364 |
+
status.className = 'status error';
|
| 365 |
+
status.textContent = 'β Connection error';
|
| 366 |
+
startBtn.disabled = false;
|
| 367 |
+
if (stream) stream.getTracks().forEach(t => t.stop());
|
| 368 |
+
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 369 |
|
| 370 |
+
ws.onclose = () => {
|
| 371 |
+
console.log('WebSocket closed');
|
| 372 |
+
};
|
| 373 |
|
| 374 |
} catch (error) {
|
| 375 |
console.error('Error:', error);
|
|
|
|
| 381 |
}
|
| 382 |
|
| 383 |
function stopStream() {
|
| 384 |
+
if (mediaRecorder && mediaRecorder.state !== 'inactive') {
|
| 385 |
+
mediaRecorder.stop();
|
| 386 |
+
}
|
| 387 |
+
if (ws) {
|
| 388 |
+
ws.close();
|
| 389 |
+
}
|
| 390 |
+
if (stream) {
|
| 391 |
+
stream.getTracks().forEach(t => t.stop());
|
| 392 |
+
}
|
| 393 |
stream = null;
|
| 394 |
document.getElementById('preview').srcObject = null;
|
| 395 |
status.className = 'status idle';
|
|
|
|
| 397 |
startBtn.disabled = false;
|
| 398 |
stopBtn.disabled = true;
|
| 399 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 400 |
</script>
|
| 401 |
</body>
|
| 402 |
</html>
|
| 403 |
`);
|
| 404 |
});
|
| 405 |
|
| 406 |
+
// Status endpoint
|
| 407 |
+
app.get("/status", (req, res) => {
|
| 408 |
+
res.json({
|
| 409 |
+
hasStream: currentStream !== null,
|
| 410 |
+
viewers: currentStream ? currentStream.viewers.size : 0,
|
| 411 |
+
streams: streams.size
|
| 412 |
+
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 413 |
});
|
| 414 |
|
| 415 |
+
// Create HTTP server
|
| 416 |
+
const server = createServer(app);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 417 |
|
| 418 |
+
// Create WebSocket server
|
| 419 |
+
const wss = new WebSocketServer({
|
| 420 |
+
server,
|
| 421 |
+
path: '/stream'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 422 |
});
|
| 423 |
|
| 424 |
+
wss.on('connection', (ws, req) => {
|
| 425 |
+
console.log('π New WebSocket connection from', req.socket.remoteAddress);
|
| 426 |
+
|
| 427 |
+
ws.on('message', (data) => {
|
| 428 |
+
try {
|
| 429 |
+
// Check if it's a text message (JSON)
|
| 430 |
+
if (data.toString().startsWith('{')) {
|
| 431 |
+
const message = JSON.parse(data.toString());
|
| 432 |
+
|
| 433 |
+
if (message.type === 'publisher') {
|
| 434 |
+
console.log('πΉ Publisher connected');
|
| 435 |
+
|
| 436 |
+
// Create new stream
|
| 437 |
+
if (currentStream) {
|
| 438 |
+
currentStream.publisher.close();
|
| 439 |
+
}
|
| 440 |
+
|
| 441 |
+
currentStream = {
|
| 442 |
+
publisher: ws,
|
| 443 |
+
viewers: new Set(),
|
| 444 |
+
metadata: { codec: message.codec }
|
| 445 |
+
};
|
| 446 |
+
|
| 447 |
+
// Notify all viewers
|
| 448 |
+
currentStream.viewers.forEach(viewer => {
|
| 449 |
+
viewer.send(JSON.stringify({
|
| 450 |
+
type: 'stream_available',
|
| 451 |
+
codec: message.codec
|
| 452 |
+
}));
|
| 453 |
+
});
|
| 454 |
+
|
| 455 |
+
ws.on('close', () => {
|
| 456 |
+
console.log('πΉ Publisher disconnected');
|
| 457 |
+
if (currentStream && currentStream.publisher === ws) {
|
| 458 |
+
currentStream.viewers.forEach(viewer => {
|
| 459 |
+
viewer.send(JSON.stringify({ type: 'no_stream' }));
|
| 460 |
+
});
|
| 461 |
+
currentStream = null;
|
| 462 |
+
}
|
| 463 |
+
});
|
| 464 |
+
|
| 465 |
+
} else if (message.type === 'viewer') {
|
| 466 |
+
console.log('ποΈ Viewer connected');
|
| 467 |
+
|
| 468 |
+
if (currentStream) {
|
| 469 |
+
currentStream.viewers.add(ws);
|
| 470 |
+
ws.send(JSON.stringify({
|
| 471 |
+
type: 'init',
|
| 472 |
+
codec: currentStream.metadata.codec
|
| 473 |
+
}));
|
| 474 |
+
ws.send(JSON.stringify({ type: 'stream_available' }));
|
| 475 |
+
} else {
|
| 476 |
+
ws.send(JSON.stringify({ type: 'no_stream' }));
|
| 477 |
+
}
|
| 478 |
+
|
| 479 |
+
ws.on('close', () => {
|
| 480 |
+
console.log('ποΈ Viewer disconnected');
|
| 481 |
+
if (currentStream) {
|
| 482 |
+
currentStream.viewers.delete(ws);
|
| 483 |
+
}
|
| 484 |
+
});
|
| 485 |
+
}
|
| 486 |
+
} else {
|
| 487 |
+
// Binary data (video chunks) from publisher
|
| 488 |
+
if (currentStream && currentStream.publisher === ws) {
|
| 489 |
+
// Broadcast to all viewers
|
| 490 |
+
currentStream.viewers.forEach(viewer => {
|
| 491 |
+
if (viewer.readyState === ws.OPEN) {
|
| 492 |
+
viewer.send(data);
|
| 493 |
+
}
|
| 494 |
+
});
|
| 495 |
+
}
|
| 496 |
+
}
|
| 497 |
+
} catch (error) {
|
| 498 |
+
console.error('β WebSocket message error:', error);
|
| 499 |
}
|
| 500 |
+
});
|
| 501 |
+
|
| 502 |
+
ws.on('error', (error) => {
|
| 503 |
+
console.error('β WebSocket error:', error);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 504 |
});
|
| 505 |
});
|
| 506 |
|
| 507 |
// Start server
|
| 508 |
const PORT = process.env.PORT || 7860;
|
| 509 |
+
server.listen(PORT, () => {
|
| 510 |
console.log(`\n${"=".repeat(50)}`);
|
| 511 |
console.log(`π Server running on port ${PORT}`);
|
| 512 |
console.log(`${"=".repeat(50)}`);
|