diff --git a/src/PeerManager.ts b/src/PeerManager.ts index 71016df..e545dfe 100644 --- a/src/PeerManager.ts +++ b/src/PeerManager.ts @@ -76,15 +76,16 @@ export class PeerManager { let peerConnection = this.peers.get(message.from); if (message.message.type === "rtc_description") { - + // let existingConnection = this.peers.get(message.from); // // We're already connected, so delete the existing connection and make a new one. - // if (peerConnection) { - // peerConnection.disconnect(); - // this.peers.delete(message.from); - // peerConnection = undefined; - // } + if (peerConnection?.rtcPeer?.connectionState === "connected") { + log("Connecting peer is already connected. Deleting existing peer connection and reconnecting."); + peerConnection.disconnect(); + this.peers.delete(message.from); + peerConnection = undefined; + } if (!peerConnection) { peerConnection = this.onConnectRequest(message); @@ -113,12 +114,19 @@ export class PeerManager { } async onHello2Received(bootstrapPeerID: string) { - if (!this.isBootstrapPeer) { - this.bootstrapPeerConnection = await this.connectToPeer(bootstrapPeerID); + + if (this.isBootstrapPeer) { + return; } - this.connectPromise?.resolve(); + if (!bootstrapPeerID) { + log("Didn't get bootstrap peer, waiting 10 seconds..."); + setTimeout(async (e:Event)=>{await this.sendHello2()}, 10_000); + return; + } + this.bootstrapPeerConnection = await this.connectToPeer(bootstrapPeerID); + this.connectPromise?.resolve(); } async sendHello2() { @@ -219,6 +227,8 @@ export class PeerManager { // TODO: What do we do if we lose connection to the bootstrap peer? // If we have other connections, it probably doesn't matter. // Eventually we want the bootstrap peer to be no different than any other peer anyway. + + // If we have no peer connections, try to connect. If connection fails, start a timer to reconnect. if (remotePeerID === this.bootstrapPeerID) { this.bootstrapPeerID = null; this.bootstrapPeerConnection = null; @@ -478,7 +488,7 @@ class PeerConnection { this.rtcPeer = new RTCPeerConnection(PeerConnection.config); this.rtcPeer.onconnectionstatechange = async (e: any) => { - log("rtcPeer: onconnectionstatechange:", this.rtcPeer?.connectionState) + log(`rtcPeer: onconnectionstatechange: ${this.rtcPeer?.connectionState}: ${this.remotePeerID}`); if (!this.rtcPeer) { throw new Error("onconnectionstatechange"); @@ -491,10 +501,20 @@ class PeerConnection { if (this.rtcPeer.connectionState === "connected") { - // let stats = await this.rtcPeer.getStats(); - // for (const stat in stats) { - // log(stat); - // } + // Check the selected candidates + const stats = await this.rtcPeer.getStats() as any; + let localIP = ''; + let remoteIP = ''; + + for (const report of stats.values()) { + if (report.type === 'transport') { + let candidatePair = stats.get(report.selectedCandidatePairId) as RTCIceCandidatePairStats; + let localCandidate = stats.get(candidatePair.localCandidateId); + let remoteCandidate = stats.get(candidatePair.remoteCandidateId); + + log("Connected candidates\n", localCandidate, remoteCandidate); + } + } } } @@ -620,7 +640,7 @@ class PeerConnection { } async disconnect() { - // this.rtcPeer?.close(); + this.rtcPeer?.close(); } send(message: any) {