fix logging console function names. Attempt to connect to initial peers

This commit is contained in:
2025-04-24 03:07:42 -07:00
parent 5a2953c876
commit 15c95e5c59
7 changed files with 135 additions and 128 deletions

View File

@@ -43,7 +43,7 @@ export class PeerManager {
return;
}
log("<-signaler:", message);
console.log.apply(null, log("<-signaler:", message));
this.websocket.send(messageJSON);
}
@@ -60,7 +60,7 @@ export class PeerManager {
throw new Error();
}
log("->signaler:", message);
console.log.apply(null, log("->signaler:", message));
if (message.type === "hello2") {
@@ -81,7 +81,7 @@ export class PeerManager {
// // We're already connected, so delete the existing connection and make a new one.
if (peerConnection?.rtcPeer?.connectionState === "connected") {
log("Connecting peer is already connected. Deleting existing peer connection and reconnecting.");
console.log.apply(null, log("Connecting peer is already connected. Deleting existing peer connection and reconnecting."));
peerConnection.disconnect();
this.peers.delete(message.from);
peerConnection = undefined;
@@ -94,7 +94,7 @@ export class PeerManager {
if (!peerConnection) {
log("Can't find peer for peer message:", message);
console.log.apply(null, log("Can't find peer for peer message:", message));
return;
}
@@ -116,12 +116,13 @@ export class PeerManager {
async onHello2Received(bootstrapPeerID: string) {
if (this.isBootstrapPeer) {
this.connectPromise?.resolve();
return;
}
if (!bootstrapPeerID) {
log("Didn't get bootstrap peer, waiting 10 seconds...");
setTimeout(async (e:Event)=>{await this.sendHello2()}, 10_000);
console.log.apply(null, log("Didn't get bootstrap peer, waiting 10 seconds..."));
setTimeout(async (e: Event) => { await this.sendHello2() }, 10_000);
return;
}
@@ -184,7 +185,7 @@ export class PeerManager {
}
this.websocket.onopen = async (event) => {
log("PeerManager:ws:onopen");
console.log.apply(null, log("peermanager:ws:onopen"));
this.sendHello2();
};
@@ -234,15 +235,13 @@ export class PeerManager {
this.bootstrapPeerConnection = null;
}
}
async disconnect(remotePeerID: string) {
let peer = this.peers.get(remotePeerID);
if (!peer) {
log(`PeerManager.disconnect: couln't find peer ${remotePeerID}`);
console.log.apply(null, log(`PeerManager.disconnect: couln't find peer ${remotePeerID}`));
return;
}
@@ -254,7 +253,7 @@ export class PeerManager {
let peer = this.peers.get(peerID);
if (!peer) {
log(`Can't find peer ${peerID}`);
console.log.apply(null, log(`Can't find peer ${peerID}`));
return;
}
@@ -265,7 +264,7 @@ export class PeerManager {
let func = this.RPC_remote.get(functionName);
if (!func) {
throw new Error();
throw new Error(`callFromRemote: got RPC we don't know about: ${functionName}, ${args}`);
}
return func(args);
@@ -292,7 +291,7 @@ export class PeerManager {
}
onMessage(remotePeerID: string, message: any) {
log(remotePeerID, message);
console.log.apply(null, log(remotePeerID, message));
}
}
@@ -339,7 +338,7 @@ interface Message {
// }
// this.websocket.onopen = async (event) => {
// log("signaler:ws:onopen");
// console.log.apply(null, log("signaler:ws:onopen");
// await this.sendHello2();
// };
@@ -361,11 +360,11 @@ interface Message {
// try {
// message = JSON.parse(messageJSON);
// } catch (e) {
// log(e);
// console.log.apply(null, log(e);
// throw new Error();
// }
// log("->signaler:", message);
// console.log.apply(null, log("->signaler:", message);
// if (message.type === "hello2") {
@@ -384,7 +383,7 @@ interface Message {
// let connection = this.peerRoutes.get(message.from_peer);
// if (!connection) {
// log("Can't find peer for peer message:", message);
// console.log.apply(null, log("Can't find peer for peer message:", message);
// return;
// }
// connection.onSignalerMessage(message);
@@ -463,11 +462,11 @@ class PeerConnection {
if (!this.dataChannel) {
throw new Error();
}
log("data channel is open!");
this.send({ type: "hello datachannel", from: this.peerManager.peerID });
console.log.apply(null, log("data channel is open!"));
this.send({ type: "hello datachannel", from: this.peerManager.peerID, to: this.remotePeerID});
// this.dataChannel?.send(`{typeHello datachannel from: ${this.peerManager.peerID}`);
log([...this.peerManager.peers.keys()]);
console.log.apply(null, log([...this.peerManager.peers.keys()]));
if (this.peerManager.isBootstrapPeer) {
this.send({ type: 'initial_peers', from: this.peerManager.peerID, peers: [...this.peerManager.peers.keys()].filter(entry => entry !== this.remotePeerID) })
@@ -478,7 +477,7 @@ class PeerConnection {
}
this.dataChannel.onmessage = (e: MessageEvent) => {
log("data channel message: ", e.data)
console.log.apply(null, log("->datachannel: ", e.data))
this.onMessage(e.data);
}
}
@@ -488,15 +487,17 @@ class PeerConnection {
this.rtcPeer = new RTCPeerConnection(PeerConnection.config);
this.rtcPeer.onconnectionstatechange = async (e: any) => {
log(`rtcPeer: onconnectionstatechange: ${this.rtcPeer?.connectionState}: ${this.remotePeerID}`);
console.log.apply(null, log(`rtcPeer: onconnectionstatechange: ${this.rtcPeer?.connectionState}: ${this.remotePeerID}`));
if (!this.rtcPeer) {
throw new Error("onconnectionstatechange");
}
// When the connection is closed, tell the peer manager that this connection has gone away
if (this.rtcPeer.connectionState === "disconnected") {
this.peerManager.onPeerDisconnected(this.remotePeerID);
// window.setTimeout(async () => { await this.peerManager.connectToPeer(this.remotePeerID) }, 10_000);
}
if (this.rtcPeer.connectionState === "connected") {
@@ -512,7 +513,7 @@ class PeerConnection {
let localCandidate = stats.get(candidatePair.localCandidateId);
let remoteCandidate = stats.get(candidatePair.remoteCandidateId);
log("Connected candidates\n", localCandidate, remoteCandidate);
console.log.apply(null, log("Connected candidates\n", localCandidate, remoteCandidate));
}
}
}
@@ -537,16 +538,16 @@ class PeerConnection {
}
// this.rtcPeer.onicecandidate = ({ candidate }) => this.signaler.send(JSON.stringify({ candidate }));
// this.rtcPeer.onicecandidate = ({ candidate }) => log(candidate);
// this.rtcPeer.onicecandidate = ({ candidate }) => console.log.apply(null, log(candidate);
this.rtcPeer.onicecandidate = ({ candidate }) => {
log(candidate);
console.log.apply(null, log(candidate));
this.sendPeerMessage(this.remotePeerID, { type: "rtc_candidate", candidate: candidate });
}
this.rtcPeer.onnegotiationneeded = async (event) => {
log("on negotiation needed fired");
console.log.apply(null, log("on negotiation needed fired"));
if (!this.rtcPeer) {
throw new Error();
@@ -644,7 +645,7 @@ class PeerConnection {
}
send(message: any) {
this.messageSuperlog && log("<-", message.type, message);
this.messageSuperlog && console.log.apply(null, log("<-datachannel:", message.type, message));
let messageJSON = JSON.stringify(message);
this.dataChannel?.send(messageJSON);
@@ -678,10 +679,10 @@ class PeerConnection {
try {
message = JSON.parse(messageJSON);
} catch (e) {
log("PeerConnection.onMessage:", e);
console.log.apply(null, log("PeerConnection.onMessage:", e));
}
this.messageSuperlog && log("->", message.type, message);
this.messageSuperlog && console.log.apply(null, log("->", message.type, message));
let type = message.type;
if (type === "rpc_response") {
@@ -704,6 +705,13 @@ class PeerConnection {
}
if (type === "initial_peers") {
for (let peerID of message.peers) {
console.log(log("Connecting to initial peer ", peerID));
this.peerManager.connectToPeer(peerID);
}
}
// this.peerManger.onMessage(this.remotePeerID, message);
}
}