peerpigeon 0.1.1 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (70) hide show
  1. package/README.md +141 -1365
  2. package/cli.js +1382 -0
  3. package/dist/peerpigeon-browser.js +12290 -0
  4. package/dist/peerpigeon-browser.js.map +7 -0
  5. package/docs/API_DOCUMENTATION.md +1945 -0
  6. package/docs/BOOTSTRAP_HUBS.md +319 -0
  7. package/docs/CLI_README.md +371 -0
  8. package/docs/DOCUMENTATION_REORGANIZATION.md +172 -0
  9. package/docs/HUB_QUICK_REF.md +107 -0
  10. package/docs/HUB_SCRIPTS.md +331 -0
  11. package/docs/HUB_SYSTEM.md +552 -0
  12. package/docs/MIGRATION_COMPLETE.md +93 -0
  13. package/docs/NETWORK_NAMESPACES.md +212 -0
  14. package/docs/README.md +107 -0
  15. package/docs/SELECTIVE_STREAMING_GUIDE.md +427 -0
  16. package/docs/TEST_CLEANUP_COMPLETE.md +80 -0
  17. package/examples/bootstrap-hub-example.js +292 -0
  18. package/examples/browser/README.md +278 -0
  19. package/examples/browser/app.js +2426 -49
  20. package/examples/browser/index.html +481 -630
  21. package/examples/browser/minimal/README.md +55 -0
  22. package/examples/browser/minimal/index.html +75 -0
  23. package/examples/browser/minimal/minimal.js +234 -0
  24. package/examples/browser/styles.css +687 -1425
  25. package/examples/browser/test-bundle.html +35 -0
  26. package/examples/browser-2/index.html +30 -0
  27. package/examples/browser-2/package.json +21 -0
  28. package/examples/browser-2/src/App-simple.vue +21 -0
  29. package/examples/browser-2/src/App.vue +420 -0
  30. package/examples/browser-2/src/components/CryptoView.vue +1661 -0
  31. package/examples/browser-2/src/components/DHTView.vue +1281 -0
  32. package/examples/browser-2/src/components/DebugView.vue +1773 -0
  33. package/examples/browser-2/src/components/MediaView.vue +1048 -0
  34. package/examples/browser-2/src/components/MessagingView.vue +775 -0
  35. package/examples/browser-2/src/components/NetworkView.vue +930 -0
  36. package/examples/browser-2/src/components/StorageView.vue +1376 -0
  37. package/examples/browser-2/src/main.js +5 -0
  38. package/examples/browser-2/src/stores/peerpigeon.js +895 -0
  39. package/examples/browser-2/vite.config.js +6 -0
  40. package/examples/hub-example.js +186 -0
  41. package/examples/multi-hub-test.js +186 -0
  42. package/examples/node/README.md +2 -2
  43. package/examples/selective-streaming-demo.html +698 -0
  44. package/examples/standard-events-demo.js +126 -0
  45. package/package.json +33 -16
  46. package/server/index.js +1032 -98
  47. package/server/start.js +49 -0
  48. package/src/ConnectionManager.js +239 -225
  49. package/src/CryptoManager.js +147 -18
  50. package/src/DebugLogger.js +0 -7
  51. package/src/DistributedStorageManager.js +224 -205
  52. package/src/EnvironmentDetector.js +109 -2
  53. package/src/EventEmitter.js +79 -1
  54. package/src/EvictionManager.js +10 -9
  55. package/src/GossipManager.js +163 -11
  56. package/src/PeerConnection.js +333 -310
  57. package/src/PeerPigeonMesh.js +577 -135
  58. package/src/SignalingClient.js +14 -243
  59. package/src/SignalingHandler.js +31 -10
  60. package/src/StorageManager.js +0 -39
  61. package/src/WebDHT-Simple.js +389 -0
  62. package/src/WebDHT.js +363 -1165
  63. package/src/browser-entry.js +40 -0
  64. package/examples/browser/assets/images/favicon.png +0 -0
  65. package/examples/browser/assets/images/pigeon.png +0 -0
  66. package/examples/browser/crypto-test.html +0 -427
  67. package/examples/browser/ui-old.js +0 -1926
  68. package/examples/browser/ui.js +0 -3391
  69. package/websocket-server/README.md +0 -247
  70. package/websocket-server/server.js +0 -391
@@ -0,0 +1,698 @@
1
+ <!DOCTYPE html>
2
+ <html lang="en">
3
+ <head>
4
+ <meta charset="UTF-8">
5
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
+ <title>PeerPigeon Selective Streaming Demo</title>
7
+ <style>
8
+ body {
9
+ font-family: Arial, sans-serif;
10
+ max-width: 1200px;
11
+ margin: 0 auto;
12
+ padding: 20px;
13
+ background: #f5f5f5;
14
+ }
15
+
16
+ .container {
17
+ background: white;
18
+ border-radius: 8px;
19
+ padding: 20px;
20
+ box-shadow: 0 2px 10px rgba(0,0,0,0.1);
21
+ margin-bottom: 20px;
22
+ }
23
+
24
+ .section {
25
+ margin-bottom: 30px;
26
+ }
27
+
28
+ .section h2 {
29
+ color: #333;
30
+ border-bottom: 2px solid #007bff;
31
+ padding-bottom: 10px;
32
+ }
33
+
34
+ .controls {
35
+ display: flex;
36
+ gap: 10px;
37
+ flex-wrap: wrap;
38
+ margin-bottom: 15px;
39
+ }
40
+
41
+ .btn {
42
+ padding: 10px 20px;
43
+ border: none;
44
+ border-radius: 5px;
45
+ background: #007bff;
46
+ color: white;
47
+ cursor: pointer;
48
+ font-size: 14px;
49
+ }
50
+
51
+ .btn:hover {
52
+ background: #0056b3;
53
+ }
54
+
55
+ .btn.secondary {
56
+ background: #6c757d;
57
+ }
58
+
59
+ .btn.secondary:hover {
60
+ background: #545b62;
61
+ }
62
+
63
+ .btn.danger {
64
+ background: #dc3545;
65
+ }
66
+
67
+ .btn.danger:hover {
68
+ background: #c82333;
69
+ }
70
+
71
+ .btn:disabled {
72
+ background: #ccc;
73
+ cursor: not-allowed;
74
+ }
75
+
76
+ select, input {
77
+ padding: 8px;
78
+ border: 1px solid #ddd;
79
+ border-radius: 4px;
80
+ margin-right: 10px;
81
+ }
82
+
83
+ select[multiple] {
84
+ min-height: 100px;
85
+ }
86
+
87
+ .status-display {
88
+ background: #f8f9fa;
89
+ border: 1px solid #dee2e6;
90
+ border-radius: 4px;
91
+ padding: 15px;
92
+ margin: 15px 0;
93
+ font-family: monospace;
94
+ }
95
+
96
+ .log {
97
+ background: #000;
98
+ color: #00ff00;
99
+ padding: 15px;
100
+ border-radius: 4px;
101
+ max-height: 300px;
102
+ overflow-y: auto;
103
+ font-family: monospace;
104
+ white-space: pre-wrap;
105
+ }
106
+
107
+ .video-container {
108
+ display: flex;
109
+ gap: 15px;
110
+ flex-wrap: wrap;
111
+ }
112
+
113
+ .video-item {
114
+ border: 2px solid #ddd;
115
+ border-radius: 8px;
116
+ overflow: hidden;
117
+ background: #000;
118
+ }
119
+
120
+ .video-item video {
121
+ width: 300px;
122
+ height: 200px;
123
+ object-fit: cover;
124
+ }
125
+
126
+ .video-label {
127
+ background: #333;
128
+ color: white;
129
+ padding: 5px 10px;
130
+ font-size: 12px;
131
+ }
132
+
133
+ .pattern-examples {
134
+ display: grid;
135
+ grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
136
+ gap: 20px;
137
+ }
138
+
139
+ .pattern-card {
140
+ border: 1px solid #ddd;
141
+ border-radius: 8px;
142
+ padding: 15px;
143
+ background: #f9f9f9;
144
+ }
145
+
146
+ .pattern-card h3 {
147
+ margin-top: 0;
148
+ color: #007bff;
149
+ }
150
+
151
+ .code-block {
152
+ background: #f4f4f4;
153
+ border: 1px solid #ddd;
154
+ border-radius: 4px;
155
+ padding: 10px;
156
+ font-family: monospace;
157
+ font-size: 12px;
158
+ margin: 10px 0;
159
+ }
160
+
161
+ .info-text {
162
+ color: #666;
163
+ font-style: italic;
164
+ margin: 10px 0;
165
+ }
166
+ </style>
167
+ </head>
168
+ <body>
169
+ <div class="container">
170
+ <h1>🎯 PeerPigeon Selective Streaming Demo</h1>
171
+ <p>This demo showcases the new selective streaming capabilities that allow you to control streaming patterns: 1:1, 1:many, many:many with individual peer control.</p>
172
+ </div>
173
+
174
+ <div class="container">
175
+ <div class="section">
176
+ <h2>🔗 Connection</h2>
177
+ <div class="controls">
178
+ <input type="text" id="signaling-url" placeholder="ws://localhost:3001" value="ws://localhost:3001">
179
+ <button class="btn" id="connect-btn">Connect</button>
180
+ <button class="btn secondary" id="disconnect-btn">Disconnect</button>
181
+ </div>
182
+ <div class="status-display">
183
+ <div><strong>Status:</strong> <span id="connection-status">Disconnected</span></div>
184
+ <div><strong>Peer ID:</strong> <span id="peer-id">Generating...</span></div>
185
+ <div><strong>Connected Peers:</strong> <span id="peer-count">0</span></div>
186
+ </div>
187
+ </div>
188
+ </div>
189
+
190
+ <div class="container">
191
+ <div class="section">
192
+ <h2>👥 Peer Selection</h2>
193
+ <div class="controls">
194
+ <select id="peer-selector" multiple size="5" style="min-width: 300px;">
195
+ <option value="" disabled>Connect to peers first...</option>
196
+ </select>
197
+ </div>
198
+ <p class="info-text">💡 Hold Ctrl/Cmd to select multiple peers for streaming control</p>
199
+ </div>
200
+ </div>
201
+
202
+ <div class="container">
203
+ <div class="section">
204
+ <h2>🎥 Media Controls</h2>
205
+ <div class="controls">
206
+ <label><input type="checkbox" id="enable-video" checked> Video</label>
207
+ <label><input type="checkbox" id="enable-audio" checked> Audio</label>
208
+ </div>
209
+
210
+ <h3>📡 Broadcasting (Traditional Mode)</h3>
211
+ <div class="controls">
212
+ <button class="btn" id="start-broadcast-btn">Start Broadcast Stream</button>
213
+ <button class="btn secondary" id="stop-broadcast-btn">Stop Broadcast</button>
214
+ </div>
215
+
216
+ <h3>🎯 Selective Streaming (New Feature)</h3>
217
+ <div class="controls">
218
+ <button class="btn" id="start-selective-btn">Start Selective Stream</button>
219
+ <button class="btn secondary" id="stop-selective-btn">Stop Selective</button>
220
+ <button class="btn secondary" id="switch-broadcast-btn">Switch to Broadcast</button>
221
+ </div>
222
+
223
+ <h3>🔧 Fine-Grained Control</h3>
224
+ <div class="controls">
225
+ <button class="btn danger" id="block-peers-btn">Block Selected Peers</button>
226
+ <button class="btn" id="allow-peers-btn">Allow Selected Peers</button>
227
+ <button class="btn secondary" id="show-status-btn">Show Streaming Status</button>
228
+ </div>
229
+ </div>
230
+ </div>
231
+
232
+ <div class="container">
233
+ <div class="section">
234
+ <h2>📊 Streaming Status</h2>
235
+ <div id="streaming-status" class="status-display">
236
+ <div>No streaming active</div>
237
+ </div>
238
+ </div>
239
+ </div>
240
+
241
+ <div class="container">
242
+ <div class="section">
243
+ <h2>🎬 Video Streams</h2>
244
+ <h3>📹 Local Stream</h3>
245
+ <div class="video-container">
246
+ <div class="video-item">
247
+ <video id="local-video" autoplay muted playsinline style="display: none;"></video>
248
+ <div class="video-label">Local (You)</div>
249
+ </div>
250
+ </div>
251
+
252
+ <h3>📺 Remote Streams</h3>
253
+ <div id="remote-streams" class="video-container">
254
+ <div class="info-text">No remote streams</div>
255
+ </div>
256
+ </div>
257
+ </div>
258
+
259
+ <div class="container">
260
+ <div class="section">
261
+ <h2>📖 Streaming Patterns Examples</h2>
262
+ <div class="pattern-examples">
263
+ <div class="pattern-card">
264
+ <h3>🎯 1:1 Streaming</h3>
265
+ <p>Stream to only one specific peer (private call)</p>
266
+ <div class="code-block">
267
+ // Select one peer and click "Start Selective Stream"
268
+ mesh.startSelectiveStream('peer-id-123', {
269
+ video: true, audio: true
270
+ });
271
+ </div>
272
+ </div>
273
+
274
+ <div class="pattern-card">
275
+ <h3>📡 1:Many Streaming</h3>
276
+ <p>Stream to multiple selected peers (group presentation)</p>
277
+ <div class="code-block">
278
+ // Select multiple peers and click "Start Selective Stream"
279
+ mesh.startSelectiveStream([
280
+ 'peer-id-123', 'peer-id-456', 'peer-id-789'
281
+ ], { video: true, audio: true });
282
+ </div>
283
+ </div>
284
+
285
+ <div class="pattern-card">
286
+ <h3>🌐 Many:Many Streaming</h3>
287
+ <p>Multiple peers streaming to each other (conference)</p>
288
+ <div class="code-block">
289
+ // Each peer calls startSelectiveStream with their
290
+ // desired targets, creating a many:many topology
291
+ peer1.startSelectiveStream(['peer2', 'peer3']);
292
+ peer2.startSelectiveStream(['peer1', 'peer3']);
293
+ peer3.startSelectiveStream(['peer1', 'peer2']);
294
+ </div>
295
+ </div>
296
+
297
+ <div class="pattern-card">
298
+ <h3>🚫 Dynamic Control</h3>
299
+ <p>Block/allow peers dynamically during streaming</p>
300
+ <div class="code-block">
301
+ // Block specific peers while keeping others
302
+ mesh.blockStreamingToPeers(['peer-id-456']);
303
+
304
+ // Re-allow blocked peers
305
+ mesh.allowStreamingToPeers(['peer-id-456']);
306
+ </div>
307
+ </div>
308
+ </div>
309
+ </div>
310
+ </div>
311
+
312
+ <div class="container">
313
+ <div class="section">
314
+ <h2>📝 Event Log</h2>
315
+ <div id="log" class="log">PeerPigeon Selective Streaming Demo loaded...\n</div>
316
+ <button class="btn secondary" onclick="document.getElementById('log').textContent = ''">Clear Log</button>
317
+ </div>
318
+ </div>
319
+
320
+ <script type="module">
321
+ import { PeerPigeonMesh } from '../src/browser-entry.js';
322
+
323
+ class SelectiveStreamingDemo {
324
+ constructor() {
325
+ this.mesh = null;
326
+ this.init();
327
+ }
328
+
329
+ async init() {
330
+ this.log('🚀 Initializing PeerPigeon with selective streaming...');
331
+
332
+ try {
333
+ this.mesh = new PeerPigeonMesh({
334
+ enableWebDHT: true,
335
+ enableCrypto: true,
336
+ enableDistributedStorage: true,
337
+ maxPeers: 5,
338
+ minPeers: 1,
339
+ autoConnect: true,
340
+ autoDiscovery: true
341
+ });
342
+
343
+ await this.mesh.init();
344
+ this.setupEventListeners();
345
+ this.setupUI();
346
+ this.updateStatus();
347
+
348
+ this.log('✅ PeerPigeon initialized with selective streaming support');
349
+ } catch (error) {
350
+ this.log(`❌ Initialization failed: ${error.message}`, 'error');
351
+ }
352
+ }
353
+
354
+ setupEventListeners() {
355
+ // Connection events
356
+ this.mesh.addEventListener('statusChanged', (data) => {
357
+ this.log(`📡 ${data.type}: ${data.message}`);
358
+ this.updateStatus();
359
+ });
360
+
361
+ this.mesh.addEventListener('peerConnected', (data) => {
362
+ this.log(`🤝 Peer connected: ${data.peerId.substring(0, 8)}...`);
363
+ this.updatePeersList();
364
+ });
365
+
366
+ this.mesh.addEventListener('peerDisconnected', (data) => {
367
+ this.log(`👋 Peer disconnected: ${data.peerId.substring(0, 8)}...`);
368
+ this.updatePeersList();
369
+ });
370
+
371
+ // Media events
372
+ this.mesh.addEventListener('localStreamStarted', (data) => {
373
+ this.log('🎥 Local stream started');
374
+ this.handleLocalStream(data.stream);
375
+ });
376
+
377
+ this.mesh.addEventListener('localStreamStopped', () => {
378
+ this.log('🛑 Local stream stopped');
379
+ this.hideLocalStream();
380
+ });
381
+
382
+ this.mesh.addEventListener('remoteStream', (data) => {
383
+ this.log(`📺 Remote stream received from ${data.peerId.substring(0, 8)}...`);
384
+ this.handleRemoteStream(data.peerId, data.stream);
385
+ });
386
+
387
+ // === NEW SELECTIVE STREAMING EVENTS ===
388
+ this.mesh.addEventListener('selectiveStreamStarted', (data) => {
389
+ this.log(`🎯 Selective ${data.streamType} streaming started to ${data.targetPeerIds.length} peer(s)`);
390
+ this.updateStreamingStatus();
391
+ });
392
+
393
+ this.mesh.addEventListener('selectiveStreamStopped', (data) => {
394
+ this.log(`🛑 Selective streaming stopped${data.returnToBroadcast ? ' (switched to broadcast)' : ''}`);
395
+ this.updateStreamingStatus();
396
+ });
397
+
398
+ this.mesh.addEventListener('broadcastStreamEnabled', () => {
399
+ this.log('📡 Broadcast streaming enabled for all peers');
400
+ this.updateStreamingStatus();
401
+ });
402
+
403
+ this.mesh.addEventListener('streamingBlockedToPeers', (data) => {
404
+ this.log(`🚫 Streaming blocked to ${data.blockedPeerIds.length} peer(s)`);
405
+ this.updateStreamingStatus();
406
+ });
407
+
408
+ this.mesh.addEventListener('streamingAllowedToPeers', (data) => {
409
+ this.log(`✅ Streaming allowed to ${data.allowedPeerIds.length} peer(s)`);
410
+ this.updateStreamingStatus();
411
+ });
412
+ }
413
+
414
+ setupUI() {
415
+ // Connection controls
416
+ document.getElementById('connect-btn').addEventListener('click', () => {
417
+ this.connect();
418
+ });
419
+
420
+ document.getElementById('disconnect-btn').addEventListener('click', () => {
421
+ this.disconnect();
422
+ });
423
+
424
+ // Media controls
425
+ document.getElementById('start-broadcast-btn').addEventListener('click', () => {
426
+ this.startBroadcastStream();
427
+ });
428
+
429
+ document.getElementById('stop-broadcast-btn').addEventListener('click', () => {
430
+ this.stopBroadcastStream();
431
+ });
432
+
433
+ document.getElementById('start-selective-btn').addEventListener('click', () => {
434
+ this.startSelectiveStream();
435
+ });
436
+
437
+ document.getElementById('stop-selective-btn').addEventListener('click', () => {
438
+ this.stopSelectiveStream();
439
+ });
440
+
441
+ document.getElementById('switch-broadcast-btn').addEventListener('click', () => {
442
+ this.switchToBroadcast();
443
+ });
444
+
445
+ document.getElementById('block-peers-btn').addEventListener('click', () => {
446
+ this.blockSelectedPeers();
447
+ });
448
+
449
+ document.getElementById('allow-peers-btn').addEventListener('click', () => {
450
+ this.allowSelectedPeers();
451
+ });
452
+
453
+ document.getElementById('show-status-btn').addEventListener('click', () => {
454
+ this.updateStreamingStatus();
455
+ });
456
+ }
457
+
458
+ async connect() {
459
+ const url = document.getElementById('signaling-url').value.trim();
460
+ if (!url) {
461
+ this.log('❌ Please enter a signaling server URL', 'error');
462
+ return;
463
+ }
464
+
465
+ try {
466
+ this.log(`🔌 Connecting to ${url}...`);
467
+ await this.mesh.connect(url);
468
+ } catch (error) {
469
+ this.log(`❌ Connection failed: ${error.message}`, 'error');
470
+ }
471
+ }
472
+
473
+ disconnect() {
474
+ this.mesh.disconnect();
475
+ this.log('🔌 Disconnected from signaling server');
476
+ this.updateStatus();
477
+ this.updatePeersList();
478
+ }
479
+
480
+ async startBroadcastStream() {
481
+ const options = this.getMediaOptions();
482
+
483
+ try {
484
+ await this.mesh.initializeMedia();
485
+ await this.mesh.startMedia(options);
486
+ this.log('📡 Broadcast streaming started to ALL connected peers');
487
+ } catch (error) {
488
+ this.log(`❌ Broadcast streaming error: ${error.message}`, 'error');
489
+ }
490
+ }
491
+
492
+ async stopBroadcastStream() {
493
+ try {
494
+ await this.mesh.stopMedia();
495
+ this.log('🛑 Broadcast streaming stopped');
496
+ } catch (error) {
497
+ this.log(`❌ Stop broadcast error: ${error.message}`, 'error');
498
+ }
499
+ }
500
+
501
+ async startSelectiveStream() {
502
+ const selectedPeers = this.getSelectedPeers();
503
+ if (selectedPeers.length === 0) {
504
+ this.log('❌ Please select at least one peer for selective streaming', 'error');
505
+ return;
506
+ }
507
+
508
+ const options = this.getMediaOptions();
509
+
510
+ try {
511
+ await this.mesh.initializeMedia();
512
+ await this.mesh.startSelectiveStream(selectedPeers, options);
513
+
514
+ const streamType = selectedPeers.length === 1 ? '1:1' : '1:many';
515
+ this.log(`🎯 Selective ${streamType} streaming started to: ${selectedPeers.map(id => id.substring(0, 8) + '...').join(', ')}`);
516
+ } catch (error) {
517
+ this.log(`❌ Selective streaming error: ${error.message}`, 'error');
518
+ }
519
+ }
520
+
521
+ async stopSelectiveStream() {
522
+ try {
523
+ await this.mesh.stopSelectiveStream(false);
524
+ this.log('🛑 Selective streaming stopped');
525
+ } catch (error) {
526
+ this.log(`❌ Stop selective streaming error: ${error.message}`, 'error');
527
+ }
528
+ }
529
+
530
+ async switchToBroadcast() {
531
+ try {
532
+ await this.mesh.stopSelectiveStream(true);
533
+ this.log('📡 Switched to broadcast mode - streaming to all peers');
534
+ } catch (error) {
535
+ this.log(`❌ Switch to broadcast error: ${error.message}`, 'error');
536
+ }
537
+ }
538
+
539
+ async blockSelectedPeers() {
540
+ const selectedPeers = this.getSelectedPeers();
541
+ if (selectedPeers.length === 0) {
542
+ this.log('❌ Please select peers to block', 'error');
543
+ return;
544
+ }
545
+
546
+ try {
547
+ await this.mesh.blockStreamingToPeers(selectedPeers);
548
+ this.log(`🚫 Blocked streaming to: ${selectedPeers.map(id => id.substring(0, 8) + '...').join(', ')}`);
549
+ } catch (error) {
550
+ this.log(`❌ Block peers error: ${error.message}`, 'error');
551
+ }
552
+ }
553
+
554
+ async allowSelectedPeers() {
555
+ const selectedPeers = this.getSelectedPeers();
556
+ if (selectedPeers.length === 0) {
557
+ this.log('❌ Please select peers to allow', 'error');
558
+ return;
559
+ }
560
+
561
+ try {
562
+ await this.mesh.allowStreamingToPeers(selectedPeers);
563
+ this.log(`✅ Allowed streaming to: ${selectedPeers.map(id => id.substring(0, 8) + '...').join(', ')}`);
564
+ } catch (error) {
565
+ this.log(`❌ Allow peers error: ${error.message}`, 'error');
566
+ }
567
+ }
568
+
569
+ getSelectedPeers() {
570
+ const selector = document.getElementById('peer-selector');
571
+ return Array.from(selector.selectedOptions).map(option => option.value);
572
+ }
573
+
574
+ getMediaOptions() {
575
+ return {
576
+ video: document.getElementById('enable-video').checked,
577
+ audio: document.getElementById('enable-audio').checked
578
+ };
579
+ }
580
+
581
+ updateStatus() {
582
+ const status = this.mesh?.getStatus() || { connected: false };
583
+ document.getElementById('connection-status').textContent = status.connected ? 'Connected' : 'Disconnected';
584
+ document.getElementById('peer-id').textContent = this.mesh?.peerId || 'Generating...';
585
+ document.getElementById('peer-count').textContent = this.mesh?.getConnectedPeerCount() || 0;
586
+ }
587
+
588
+ updatePeersList() {
589
+ const selector = document.getElementById('peer-selector');
590
+ const currentSelections = Array.from(selector.selectedOptions).map(option => option.value);
591
+
592
+ selector.innerHTML = '';
593
+
594
+ if (!this.mesh) return;
595
+
596
+ const connectedPeers = this.mesh.getConnectedPeerCount();
597
+ if (connectedPeers === 0) {
598
+ const option = document.createElement('option');
599
+ option.value = '';
600
+ option.disabled = true;
601
+ option.textContent = 'No peers connected...';
602
+ selector.appendChild(option);
603
+ return;
604
+ }
605
+
606
+ // Get connected peer IDs (this might need to be implemented)
607
+ const peers = this.mesh.getPeers?.() || [];
608
+ peers.forEach(peerId => {
609
+ const option = document.createElement('option');
610
+ option.value = peerId;
611
+ option.textContent = `${peerId.substring(0, 8)}...${peerId.substring(-8)}`;
612
+
613
+ if (currentSelections.includes(peerId)) {
614
+ option.selected = true;
615
+ }
616
+
617
+ selector.appendChild(option);
618
+ });
619
+ }
620
+
621
+ updateStreamingStatus() {
622
+ if (!this.mesh || !this.mesh.getStreamingStatus) {
623
+ document.getElementById('streaming-status').innerHTML = '<div>Streaming methods not available</div>';
624
+ return;
625
+ }
626
+
627
+ try {
628
+ const streamingPeers = this.mesh.getStreamingPeers();
629
+ const blockedPeers = this.mesh.getBlockedStreamingPeers();
630
+ const isStreamingToAll = this.mesh.isStreamingToAll();
631
+
632
+ const statusDiv = document.getElementById('streaming-status');
633
+ statusDiv.innerHTML = `
634
+ <div><strong>Mode:</strong> ${isStreamingToAll ? 'Broadcast (All Peers)' : 'Selective'}</div>
635
+ <div><strong>Streaming to:</strong> ${streamingPeers.length} peer(s)</div>
636
+ <div><strong>Blocked:</strong> ${blockedPeers.length} peer(s)</div>
637
+ <div><strong>Active Peers:</strong> ${streamingPeers.map(id => id.substring(0, 8) + '...').join(', ') || 'None'}</div>
638
+ ${blockedPeers.length > 0 ? `<div><strong>Blocked Peers:</strong> ${blockedPeers.map(id => id.substring(0, 8) + '...').join(', ')}</div>` : ''}
639
+ `;
640
+ } catch (error) {
641
+ document.getElementById('streaming-status').innerHTML = `<div>Error getting streaming status: ${error.message}</div>`;
642
+ }
643
+ }
644
+
645
+ handleLocalStream(stream) {
646
+ const video = document.getElementById('local-video');
647
+ video.srcObject = stream;
648
+ video.style.display = 'block';
649
+ }
650
+
651
+ hideLocalStream() {
652
+ const video = document.getElementById('local-video');
653
+ video.srcObject = null;
654
+ video.style.display = 'none';
655
+ }
656
+
657
+ handleRemoteStream(peerId, stream) {
658
+ const container = document.getElementById('remote-streams');
659
+
660
+ // Remove "no streams" message
661
+ if (container.querySelector('.info-text')) {
662
+ container.innerHTML = '';
663
+ }
664
+
665
+ // Remove existing stream from this peer
666
+ const existing = document.getElementById(`remote-${peerId}`);
667
+ if (existing) {
668
+ existing.remove();
669
+ }
670
+
671
+ // Add new stream
672
+ const videoItem = document.createElement('div');
673
+ videoItem.className = 'video-item';
674
+ videoItem.id = `remote-${peerId}`;
675
+ videoItem.innerHTML = `
676
+ <video autoplay playsinline></video>
677
+ <div class="video-label">${peerId.substring(0, 8)}...</div>
678
+ `;
679
+
680
+ const video = videoItem.querySelector('video');
681
+ video.srcObject = stream;
682
+
683
+ container.appendChild(videoItem);
684
+ }
685
+
686
+ log(message, type = 'info') {
687
+ const logDiv = document.getElementById('log');
688
+ const timestamp = new Date().toLocaleTimeString();
689
+ logDiv.textContent += `[${timestamp}] ${message}\n`;
690
+ logDiv.scrollTop = logDiv.scrollHeight;
691
+ }
692
+ }
693
+
694
+ // Initialize the demo
695
+ new SelectiveStreamingDemo();
696
+ </script>
697
+ </body>
698
+ </html>