Print connection candidate stats on connect. Retry if we cant connect to the bootstrap node. Need to do the same for the intial websocket connection.

This commit is contained in:
2025-04-21 00:56:25 -07:00
parent f6bf55f13c
commit 5a2953c876

View File

@@ -76,15 +76,16 @@ export class PeerManager {
let peerConnection = this.peers.get(message.from); let peerConnection = this.peers.get(message.from);
if (message.message.type === "rtc_description") { if (message.message.type === "rtc_description") {
// let existingConnection = this.peers.get(message.from); // let existingConnection = this.peers.get(message.from);
// // We're already connected, so delete the existing connection and make a new one. // // We're already connected, so delete the existing connection and make a new one.
// if (peerConnection) { if (peerConnection?.rtcPeer?.connectionState === "connected") {
// peerConnection.disconnect(); log("Connecting peer is already connected. Deleting existing peer connection and reconnecting.");
// this.peers.delete(message.from); peerConnection.disconnect();
// peerConnection = undefined; this.peers.delete(message.from);
// } peerConnection = undefined;
}
if (!peerConnection) { if (!peerConnection) {
peerConnection = this.onConnectRequest(message); peerConnection = this.onConnectRequest(message);
@@ -113,12 +114,19 @@ export class PeerManager {
} }
async onHello2Received(bootstrapPeerID: string) { 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() { async sendHello2() {
@@ -219,6 +227,8 @@ export class PeerManager {
// TODO: What do we do if we lose connection to the bootstrap peer? // TODO: What do we do if we lose connection to the bootstrap peer?
// If we have other connections, it probably doesn't matter. // 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. // 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) { if (remotePeerID === this.bootstrapPeerID) {
this.bootstrapPeerID = null; this.bootstrapPeerID = null;
this.bootstrapPeerConnection = null; this.bootstrapPeerConnection = null;
@@ -478,7 +488,7 @@ class PeerConnection {
this.rtcPeer = new RTCPeerConnection(PeerConnection.config); this.rtcPeer = new RTCPeerConnection(PeerConnection.config);
this.rtcPeer.onconnectionstatechange = async (e: any) => { this.rtcPeer.onconnectionstatechange = async (e: any) => {
log("rtcPeer: onconnectionstatechange:", this.rtcPeer?.connectionState) log(`rtcPeer: onconnectionstatechange: ${this.rtcPeer?.connectionState}: ${this.remotePeerID}`);
if (!this.rtcPeer) { if (!this.rtcPeer) {
throw new Error("onconnectionstatechange"); throw new Error("onconnectionstatechange");
@@ -491,10 +501,20 @@ class PeerConnection {
if (this.rtcPeer.connectionState === "connected") { if (this.rtcPeer.connectionState === "connected") {
// let stats = await this.rtcPeer.getStats(); // Check the selected candidates
// for (const stat in stats) { const stats = await this.rtcPeer.getStats() as any;
// log(stat); 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() { async disconnect() {
// this.rtcPeer?.close(); this.rtcPeer?.close();
} }
send(message: any) { send(message: any) {