peerpigeon 0.0.5 → 0.0.7
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.
- package/README.md +190 -12
- package/examples/browser/app.js +40 -40
- package/examples/browser/crypto-test.html +427 -0
- package/examples/browser/index.html +161 -0
- package/examples/browser/styles.css +216 -0
- package/examples/browser/ui-old.js +1704 -1708
- package/examples/browser/ui.js +2226 -1692
- package/examples/nativescript/README.md +73 -0
- package/examples/nativescript/app.js +152 -0
- package/examples/nativescript/package.json +27 -0
- package/examples/node/test-crypto-media.js +137 -0
- package/examples/node/test-crypto.js +146 -0
- package/examples/node/test-node.js +114 -115
- package/package.json +16 -13
- package/src/CleanupManager.js +154 -154
- package/src/ConnectionManager.js +772 -753
- package/src/CryptoManager.js +612 -0
- package/src/EnvironmentDetector.js +457 -384
- package/src/EventEmitter.js +19 -19
- package/src/EvictionManager.js +211 -211
- package/src/GossipManager.js +447 -366
- package/src/MediaManager.js +216 -214
- package/src/MeshOptimizer.js +165 -166
- package/src/PeerConnection.js +847 -941
- package/src/PeerDiscovery.js +255 -259
- package/src/PeerPigeonMesh.js +1100 -721
- package/src/SignalingClient.js +517 -518
- package/src/SignalingHandler.js +295 -295
- package/src/StorageManager.js +118 -104
- package/src/TimerUtils.js +29 -29
- package/src/WebDHT.js +1123 -948
- package/websocket-server/server.js +360 -362
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
/**
|
|
4
4
|
* Node.js Test for PeerPigeon Mesh Networking
|
|
5
5
|
* Demonstrates P2P mesh functionality in Node.js environment
|
|
6
|
-
*
|
|
6
|
+
*
|
|
7
7
|
* Prerequisites:
|
|
8
8
|
* - npm install ws @koush/wrtc
|
|
9
9
|
* - Start signaling server: npm run server
|
|
@@ -25,129 +25,128 @@ const TEST_DURATION = 30000; // 30 seconds
|
|
|
25
25
|
console.log('� PeerPigeon Node.js Mesh Test\n');
|
|
26
26
|
|
|
27
27
|
async function createPeer(peerName, peerId = null) {
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
28
|
+
const mesh = new PeerPigeonMesh({
|
|
29
|
+
peerId,
|
|
30
|
+
enableWebDHT: true,
|
|
31
|
+
ignoreEnvironmentErrors: true
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
// Set up event handlers
|
|
35
|
+
mesh.addEventListener('statusChanged', (event) => {
|
|
36
|
+
console.log(`[${peerName}] Status: ${event.type} - ${event.message || ''}`);
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
mesh.addEventListener('peerConnected', (event) => {
|
|
40
|
+
console.log(`[${peerName}] 🔗 Connected to peer: ${event.peerId.substring(0, 8)}...`);
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
mesh.addEventListener('peerDisconnected', (event) => {
|
|
44
|
+
console.log(`[${peerName}] 💔 Disconnected from peer: ${event.peerId.substring(0, 8)}... (${event.reason})`);
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
mesh.addEventListener('messageReceived', (event) => {
|
|
48
|
+
const fromPeerId = event.from || 'unknown';
|
|
49
|
+
console.log(`[${peerName}] 📨 Received: "${event.content}" from ${fromPeerId.substring(0, 8)}...`);
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
// Initialize the mesh
|
|
53
|
+
await mesh.init();
|
|
54
|
+
console.log(`[${peerName}] Initialized with ID: ${mesh.peerId.substring(0, 8)}...`);
|
|
55
|
+
|
|
56
|
+
// Display environment info
|
|
57
|
+
const envReport = mesh.getEnvironmentReport();
|
|
58
|
+
console.log(`[${peerName}] Environment: Node.js ${envReport.node?.version}, WebRTC: ${envReport.capabilities.webrtc}, WebSocket: ${envReport.capabilities.webSocket}`);
|
|
59
|
+
|
|
60
|
+
return mesh;
|
|
61
61
|
}
|
|
62
62
|
|
|
63
63
|
async function runTest() {
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
}
|
|
64
|
+
try {
|
|
65
|
+
console.log('Creating two mesh peers...\n');
|
|
66
|
+
|
|
67
|
+
// Create two peers
|
|
68
|
+
const peer1 = await createPeer('PEER-1');
|
|
69
|
+
const peer2 = await createPeer('PEER-2');
|
|
70
|
+
|
|
71
|
+
console.log('\nConnecting to signaling server...\n');
|
|
72
|
+
|
|
73
|
+
// Connect both peers to signaling server
|
|
74
|
+
await peer1.connect(SIGNALING_URL);
|
|
75
|
+
await peer2.connect(SIGNALING_URL);
|
|
76
|
+
|
|
77
|
+
// Wait for connections to establish
|
|
78
|
+
await new Promise(resolve => setTimeout(resolve, 2000));
|
|
79
|
+
|
|
80
|
+
console.log('\nTesting message exchange...\n');
|
|
81
|
+
|
|
82
|
+
// Test messaging
|
|
83
|
+
let messageCount = 0;
|
|
84
|
+
const sendMessage = () => {
|
|
85
|
+
messageCount++;
|
|
86
|
+
const message = `Hello from Node.js test #${messageCount}`;
|
|
87
|
+
|
|
88
|
+
if (messageCount % 2 === 1) {
|
|
89
|
+
console.log(`[PEER-1] 📤 Sending: "${message}"`);
|
|
90
|
+
peer1.sendMessage(message);
|
|
91
|
+
} else {
|
|
92
|
+
console.log(`[PEER-2] 📤 Sending: "${message}"`);
|
|
93
|
+
peer2.sendMessage(message);
|
|
94
|
+
}
|
|
95
|
+
};
|
|
96
|
+
|
|
97
|
+
// Send messages every 3 seconds
|
|
98
|
+
const messageInterval = setInterval(sendMessage, 3000);
|
|
99
|
+
sendMessage(); // Send first message immediately
|
|
100
|
+
|
|
101
|
+
// Test DHT functionality
|
|
102
|
+
setTimeout(async () => {
|
|
103
|
+
console.log('\nTesting DHT functionality...\n');
|
|
104
|
+
try {
|
|
105
|
+
await peer1.dhtPut('test-key', { data: 'Hello from Node.js DHT!', timestamp: Date.now() });
|
|
106
|
+
console.log('[PEER-1] 📝 Stored data in DHT');
|
|
107
|
+
|
|
108
|
+
const value = await peer2.dhtGet('test-key');
|
|
109
|
+
console.log('[PEER-2] 📖 Retrieved from DHT:', value);
|
|
110
|
+
} catch (error) {
|
|
111
|
+
console.error('DHT test failed:', error.message);
|
|
112
|
+
}
|
|
113
|
+
}, 5000);
|
|
114
|
+
|
|
115
|
+
// Display stats periodically
|
|
116
|
+
const statsInterval = setInterval(() => {
|
|
117
|
+
const peer1ConnectedCount = peer1.getConnectedPeerCount();
|
|
118
|
+
const peer2ConnectedCount = peer2.getConnectedPeerCount();
|
|
119
|
+
console.log('\n--- Stats ---');
|
|
120
|
+
console.log(`PEER-1: ${peer1ConnectedCount} connected peers`);
|
|
121
|
+
console.log(`PEER-2: ${peer2ConnectedCount} connected peers`);
|
|
122
|
+
}, 10000);
|
|
123
|
+
|
|
124
|
+
// Run test for specified duration
|
|
125
|
+
setTimeout(() => {
|
|
126
|
+
console.log('\n🏁 Test completed, cleaning up...\n');
|
|
127
|
+
clearInterval(messageInterval);
|
|
128
|
+
clearInterval(statsInterval);
|
|
129
|
+
|
|
130
|
+
peer1.disconnect();
|
|
131
|
+
peer2.disconnect();
|
|
132
|
+
|
|
133
|
+
process.exit(0);
|
|
134
|
+
}, TEST_DURATION);
|
|
135
|
+
} catch (error) {
|
|
136
|
+
console.error('❌ Test failed:', error);
|
|
137
|
+
process.exit(1);
|
|
138
|
+
}
|
|
140
139
|
}
|
|
141
140
|
|
|
142
141
|
// Check if signaling server is running
|
|
143
142
|
console.log(`Checking signaling server at ${SIGNALING_URL}...`);
|
|
144
143
|
const testWs = new WebSocket(SIGNALING_URL);
|
|
145
144
|
testWs.on('open', () => {
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
145
|
+
testWs.close();
|
|
146
|
+
console.log('✅ Signaling server is running\n');
|
|
147
|
+
runTest();
|
|
149
148
|
});
|
|
150
149
|
testWs.on('error', () => {
|
|
151
|
-
|
|
152
|
-
|
|
150
|
+
console.error('❌ Signaling server not running. Start it with: npm run server');
|
|
151
|
+
process.exit(1);
|
|
153
152
|
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "peerpigeon",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.7",
|
|
4
4
|
"description": "WebRTC-based peer-to-peer mesh networking library with intelligent routing and signaling server",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"type": "module",
|
|
@@ -13,9 +13,10 @@
|
|
|
13
13
|
"dev": "http-server . -p 8080 -c-1",
|
|
14
14
|
"start": "node websocket-server/server.js",
|
|
15
15
|
"server": "node websocket-server/server.js",
|
|
16
|
-
"
|
|
17
|
-
"lint": "
|
|
18
|
-
"
|
|
16
|
+
"lint": "eslint src/ examples/ websocket-server/ --ext .js",
|
|
17
|
+
"lint:fix": "eslint src/ examples/ websocket-server/ --ext .js --fix",
|
|
18
|
+
"ci": "npm run lint",
|
|
19
|
+
"prepublishOnly": "npm run ci"
|
|
19
20
|
},
|
|
20
21
|
"files": [
|
|
21
22
|
"index.js",
|
|
@@ -44,7 +45,8 @@
|
|
|
44
45
|
"gossip",
|
|
45
46
|
"mesh-network",
|
|
46
47
|
"xor-routing",
|
|
47
|
-
"webrtc-mesh"
|
|
48
|
+
"webrtc-mesh",
|
|
49
|
+
"nativescript"
|
|
48
50
|
],
|
|
49
51
|
"author": "Daniel Raeder",
|
|
50
52
|
"license": "MIT",
|
|
@@ -53,18 +55,19 @@
|
|
|
53
55
|
},
|
|
54
56
|
"homepage": "https://github.com/draeder/peerpigeon#readme",
|
|
55
57
|
"dependencies": {
|
|
58
|
+
"@koush/wrtc": "^0.5.3",
|
|
59
|
+
"unsea": "^1.1.0",
|
|
56
60
|
"ws": "^8.18.0",
|
|
57
|
-
"
|
|
61
|
+
"yamljs": "^0.3.0"
|
|
58
62
|
},
|
|
59
63
|
"devDependencies": {
|
|
60
|
-
"
|
|
64
|
+
"eslint": "^8.57.1",
|
|
65
|
+
"eslint-config-standard": "^17.1.0",
|
|
66
|
+
"http-server": "^14.1.1",
|
|
67
|
+
"jsdoc": "^4.0.4",
|
|
68
|
+
"swagger-ui-express": "^5.0.1"
|
|
61
69
|
},
|
|
62
70
|
"engines": {
|
|
63
71
|
"node": ">=14.0.0"
|
|
64
|
-
}
|
|
65
|
-
"browserslist": [
|
|
66
|
-
"last 2 versions",
|
|
67
|
-
"not dead",
|
|
68
|
-
"> 1%"
|
|
69
|
-
]
|
|
72
|
+
}
|
|
70
73
|
}
|
package/src/CleanupManager.js
CHANGED
|
@@ -4,178 +4,178 @@ import { EventEmitter } from './EventEmitter.js';
|
|
|
4
4
|
* Manages cleanup operations for signaling data and peer connections
|
|
5
5
|
*/
|
|
6
6
|
export class CleanupManager extends EventEmitter {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
7
|
+
constructor(mesh) {
|
|
8
|
+
super();
|
|
9
|
+
this.mesh = mesh;
|
|
10
|
+
this.cleanupInProgress = new Set();
|
|
11
|
+
}
|
|
12
12
|
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
this.cleanupInProgress.add(peerId);
|
|
21
|
-
|
|
22
|
-
try {
|
|
23
|
-
console.log('Cleaning up signaling data for', peerId);
|
|
24
|
-
|
|
25
|
-
const response = await this.mesh.signalingClient.sendSignalingMessage({
|
|
26
|
-
type: 'cleanup',
|
|
27
|
-
data: {
|
|
28
|
-
peerId: this.mesh.peerId,
|
|
29
|
-
targetPeerId: peerId,
|
|
30
|
-
timestamp: Date.now(),
|
|
31
|
-
reason: 'connection_established'
|
|
32
|
-
},
|
|
33
|
-
targetPeerId: peerId
|
|
34
|
-
});
|
|
35
|
-
|
|
36
|
-
// Only log if something was actually cleaned up
|
|
37
|
-
if (response.cleaned && (response.cleaned.signaling > 0 || response.cleaned.discovery > 0)) {
|
|
38
|
-
console.log('Signaling cleanup completed for', peerId, response);
|
|
39
|
-
this.mesh.emit('statusChanged', { type: 'info', message: `Cleaned up signaling data with ${peerId.substring(0, 8)}...` });
|
|
40
|
-
}
|
|
41
|
-
} catch (error) {
|
|
42
|
-
console.error('Failed to cleanup signaling data for', peerId, error);
|
|
43
|
-
// Don't show error to user as this is background cleanup
|
|
44
|
-
} finally {
|
|
45
|
-
this.cleanupInProgress.delete(peerId);
|
|
46
|
-
}
|
|
13
|
+
async cleanupSignalingData(peerId) {
|
|
14
|
+
// Prevent duplicate cleanup calls
|
|
15
|
+
if (this.cleanupInProgress.has(peerId)) {
|
|
16
|
+
console.log('Cleanup already in progress for', peerId);
|
|
17
|
+
return;
|
|
47
18
|
}
|
|
48
19
|
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
}
|
|
20
|
+
this.cleanupInProgress.add(peerId);
|
|
21
|
+
|
|
22
|
+
try {
|
|
23
|
+
console.log('Cleaning up signaling data for', peerId);
|
|
24
|
+
|
|
25
|
+
const response = await this.mesh.signalingClient.sendSignalingMessage({
|
|
26
|
+
type: 'cleanup',
|
|
27
|
+
data: {
|
|
28
|
+
peerId: this.mesh.peerId,
|
|
29
|
+
targetPeerId: peerId,
|
|
30
|
+
timestamp: Date.now(),
|
|
31
|
+
reason: 'connection_established'
|
|
32
|
+
},
|
|
33
|
+
targetPeerId: peerId
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
// Only log if something was actually cleaned up
|
|
37
|
+
if (response.cleaned && (response.cleaned.signaling > 0 || response.cleaned.discovery > 0)) {
|
|
38
|
+
console.log('Signaling cleanup completed for', peerId, response);
|
|
39
|
+
this.mesh.emit('statusChanged', { type: 'info', message: `Cleaned up signaling data with ${peerId.substring(0, 8)}...` });
|
|
40
|
+
}
|
|
41
|
+
} catch (error) {
|
|
42
|
+
console.error('Failed to cleanup signaling data for', peerId, error);
|
|
43
|
+
// Don't show error to user as this is background cleanup
|
|
44
|
+
} finally {
|
|
45
|
+
this.cleanupInProgress.delete(peerId);
|
|
69
46
|
}
|
|
47
|
+
}
|
|
70
48
|
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
} catch (error) {
|
|
87
|
-
console.log('Failed to send cleanup-all message:', error.message);
|
|
88
|
-
}
|
|
49
|
+
async cleanupAllSignalingData() {
|
|
50
|
+
if (this.mesh.signalingClient && this.mesh.peerId) {
|
|
51
|
+
try {
|
|
52
|
+
const response = await this.mesh.signalingClient.sendSignalingMessage({
|
|
53
|
+
type: 'cleanup-all',
|
|
54
|
+
data: {
|
|
55
|
+
peerId: this.mesh.peerId,
|
|
56
|
+
timestamp: Date.now(),
|
|
57
|
+
reason: 'peer_cleanup'
|
|
58
|
+
}
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
// Only log if something was actually cleaned
|
|
62
|
+
if (response.cleaned > 0) {
|
|
63
|
+
console.log(`Cleaned up ${response.cleaned} stale signaling items`);
|
|
89
64
|
}
|
|
65
|
+
} catch (error) {
|
|
66
|
+
console.log('Failed to cleanup all signaling data:', error.message);
|
|
67
|
+
}
|
|
90
68
|
}
|
|
69
|
+
}
|
|
91
70
|
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
71
|
+
cleanupAllSignalingDataSync() {
|
|
72
|
+
if (this.mesh.signalingClient && this.mesh.connected && this.mesh.peerId) {
|
|
73
|
+
try {
|
|
74
|
+
// Send cleanup message via WebSocket synchronously
|
|
75
|
+
this.mesh.signalingClient.sendSignalingMessage({
|
|
76
|
+
type: 'cleanup-all',
|
|
77
|
+
data: {
|
|
78
|
+
peerId: this.mesh.peerId,
|
|
79
|
+
timestamp: Date.now(),
|
|
80
|
+
reason: 'browser_unload'
|
|
81
|
+
}
|
|
82
|
+
}).catch(error => {
|
|
83
|
+
console.log('Failed to send cleanup-all message:', error.message);
|
|
84
|
+
});
|
|
85
|
+
console.log('Cleanup-all message sent via WebSocket');
|
|
86
|
+
} catch (error) {
|
|
87
|
+
console.log('Failed to send cleanup-all message:', error.message);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
}
|
|
98
91
|
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
// Use safer iteration to avoid race conditions
|
|
106
|
-
if (this.mesh.connectionManager && this.mesh.connectionManager.peers) {
|
|
107
|
-
const peerEntries = Array.from(this.mesh.connectionManager.peers.entries());
|
|
108
|
-
|
|
109
|
-
for (const [peerId, peerConnection] of peerEntries) {
|
|
110
|
-
try {
|
|
111
|
-
if (peerConnection && peerConnection.getStatus && peerConnection.getStatus() === 'connected') {
|
|
112
|
-
cleanupPromises.push(this.cleanupSignalingData(peerId));
|
|
113
|
-
}
|
|
114
|
-
} catch (error) {
|
|
115
|
-
console.log(`Error checking peer ${peerId} status during cleanup:`, error.message);
|
|
116
|
-
}
|
|
117
|
-
}
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
// Wait for all peer-specific cleanups to complete
|
|
121
|
-
if (cleanupPromises.length > 0) {
|
|
122
|
-
await Promise.allSettled(cleanupPromises);
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
// Then do a comprehensive cleanup
|
|
126
|
-
await this.cleanupAllSignalingData();
|
|
127
|
-
|
|
128
|
-
console.log('Manual cleanup completed for all connected peers');
|
|
129
|
-
this.mesh.emit('statusChanged', { type: 'info', message: 'Cleaned up stale signaling data' });
|
|
130
|
-
} catch (error) {
|
|
131
|
-
console.error('Manual cleanup failed:', error);
|
|
132
|
-
this.mesh.emit('statusChanged', { type: 'error', message: 'Failed to cleanup signaling data' });
|
|
133
|
-
}
|
|
92
|
+
// Manual cleanup method for already-connected mesh networks
|
|
93
|
+
async cleanupStaleSignalingData() {
|
|
94
|
+
if (!this.mesh.signalingClient || !this.mesh.connected) {
|
|
95
|
+
console.log('Cannot cleanup - not connected to signaling server');
|
|
96
|
+
return;
|
|
134
97
|
}
|
|
135
98
|
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
99
|
+
console.log('Manually cleaning up stale signaling data for all connected peers...');
|
|
100
|
+
|
|
101
|
+
try {
|
|
102
|
+
// Clean up signaling data for each connected peer
|
|
103
|
+
const cleanupPromises = [];
|
|
104
|
+
|
|
105
|
+
// Use safer iteration to avoid race conditions
|
|
106
|
+
if (this.mesh.connectionManager && this.mesh.connectionManager.peers) {
|
|
107
|
+
const peerEntries = Array.from(this.mesh.connectionManager.peers.entries());
|
|
108
|
+
|
|
109
|
+
for (const [peerId, peerConnection] of peerEntries) {
|
|
110
|
+
try {
|
|
111
|
+
if (peerConnection && peerConnection.getStatus && peerConnection.getStatus() === 'connected') {
|
|
112
|
+
cleanupPromises.push(this.cleanupSignalingData(peerId));
|
|
144
113
|
}
|
|
114
|
+
} catch (error) {
|
|
115
|
+
console.log(`Error checking peer ${peerId} status during cleanup:`, error.message);
|
|
116
|
+
}
|
|
145
117
|
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
// Wait for all peer-specific cleanups to complete
|
|
121
|
+
if (cleanupPromises.length > 0) {
|
|
122
|
+
await Promise.allSettled(cleanupPromises);
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
// Then do a comprehensive cleanup
|
|
126
|
+
await this.cleanupAllSignalingData();
|
|
127
|
+
|
|
128
|
+
console.log('Manual cleanup completed for all connected peers');
|
|
129
|
+
this.mesh.emit('statusChanged', { type: 'info', message: 'Cleaned up stale signaling data' });
|
|
130
|
+
} catch (error) {
|
|
131
|
+
console.error('Manual cleanup failed:', error);
|
|
132
|
+
this.mesh.emit('statusChanged', { type: 'error', message: 'Failed to cleanup signaling data' });
|
|
146
133
|
}
|
|
134
|
+
}
|
|
147
135
|
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
}
|
|
158
|
-
}
|
|
136
|
+
sendGoodbyeMessageSync() {
|
|
137
|
+
if (this.mesh.signalingClient && this.mesh.connected) {
|
|
138
|
+
try {
|
|
139
|
+
// For WebSocket, send goodbye message synchronously
|
|
140
|
+
this.mesh.signalingClient.sendGoodbyeMessage();
|
|
141
|
+
console.log('Goodbye message sent via WebSocket');
|
|
142
|
+
} catch (error) {
|
|
143
|
+
console.log('Failed to send goodbye message:', error.message);
|
|
144
|
+
}
|
|
159
145
|
}
|
|
146
|
+
}
|
|
160
147
|
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
// DO NOT cleanup on focus loss or visibility change (breaks multi-tab usage)
|
|
172
|
-
window.addEventListener('beforeunload', handleUnload);
|
|
173
|
-
window.addEventListener('unload', handleUnload);
|
|
174
|
-
window.addEventListener('pagehide', handleUnload);
|
|
175
|
-
}
|
|
148
|
+
async sendGoodbyeMessage() {
|
|
149
|
+
if (this.mesh.signalingClient && this.mesh.connected) {
|
|
150
|
+
try {
|
|
151
|
+
await this.mesh.signalingClient.sendSignalingMessage({
|
|
152
|
+
type: 'goodbye',
|
|
153
|
+
data: { peerId: this.mesh.peerId, timestamp: Date.now() }
|
|
154
|
+
});
|
|
155
|
+
} catch (error) {
|
|
156
|
+
console.log('Failed to send goodbye message:', error.message);
|
|
157
|
+
}
|
|
176
158
|
}
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
setupUnloadHandlers() {
|
|
162
|
+
// Handle page unload/refresh - ONLY clean up ALL peer data on actual unload
|
|
163
|
+
if (typeof window !== 'undefined') {
|
|
164
|
+
const handleUnload = () => {
|
|
165
|
+
console.log('Page unloading - cleaning up ALL peer data');
|
|
166
|
+
this.sendGoodbyeMessageSync();
|
|
167
|
+
this.cleanupAllSignalingDataSync();
|
|
168
|
+
};
|
|
177
169
|
|
|
178
|
-
|
|
179
|
-
|
|
170
|
+
// Only trigger cleanup on actual page unload scenarios
|
|
171
|
+
// DO NOT cleanup on focus loss or visibility change (breaks multi-tab usage)
|
|
172
|
+
window.addEventListener('beforeunload', handleUnload);
|
|
173
|
+
window.addEventListener('unload', handleUnload);
|
|
174
|
+
window.addEventListener('pagehide', handleUnload);
|
|
180
175
|
}
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
cleanup() {
|
|
179
|
+
this.cleanupInProgress.clear();
|
|
180
|
+
}
|
|
181
181
|
}
|