working on replies

This commit is contained in:
2026-04-15 06:05:46 +00:00
parent 0d392c90cc
commit c1de283fb6
15 changed files with 436 additions and 211 deletions

View File

@@ -51,6 +51,14 @@ export class PeerManager {
this.messageSuperlog && console.log.apply(null, log("<-signaler:", message));
this.websocket.send(messageJSON);
}
onPeerMessageFromPeer(remotePeerID, messageJSON) {
let targetPeer = this.peers.get(messageJSON.to);
if (!targetPeer) {
console.log.apply(null, log("[PeerManager] Coulnd't find peer for onPeerMessageFromPeer:", messageJSON.to));
return;
}
targetPeer.send(messageJSON);
}
onWebsocketMessage(event) {
let messageJSON = event.data;
let message = null;
@@ -82,9 +90,7 @@ export class PeerManager {
if (!peerConnection) {
let remotePeerID = message.from;
let newPeer = new PeerConnection(this, remotePeerID, this.websocketSendPeerMessage.bind(this));
if (this._isBootstrapPeer) {
newPeer.setPolite(false);
}
newPeer.setPoliteFromID(remotePeerID, true);
peerConnection = newPeer;
this.peers.set(newPeer.remotePeerID, newPeer);
this.onConnectRequest(newPeer);
@@ -163,7 +169,6 @@ export class PeerManager {
// message: { type: "get_posts_for_user", post_ids: postIds, user_id: message.user_id } }
}
constructor(userID, peerID, isBootstrapPeer) {
// private signaler: Signaler;
this.searchQueryFunctions = new Map();
this.RPC_remote = new Map();
this.rpc = {};
@@ -182,12 +187,6 @@ export class PeerManager {
this.watchdogInterval = null;
this.reconnectTimer = null;
this.peerStateSuperlog = true;
// async watchdog() {
// // Check that we're connected to at least N peers. If not, reconnect to the bootstrap server.
// if (this.peers.size === 0) {
// await this.sendHello2();
// }
// }
this.animals = ['shrew', 'jerboa', 'lemur', 'weasel', 'possum', 'possum', 'marmoset', 'planigale', 'mole', 'narwhal'];
this.adjectives = ['snazzy', 'whimsical', 'jazzy', 'bonkers', 'wobbly', 'spiffy', 'chirpy', 'zesty', 'bubbly', 'perky', 'sassy'];
this.snakes = ['mamba', 'cobra', 'python', 'viper', 'krait', 'sidewinder', 'constrictor', 'boa', 'asp', 'anaconda', 'krait'];
@@ -417,6 +416,10 @@ class PeerConnection {
setPolite(polite) {
this.polite = polite;
}
setPoliteFromID(peerID, remote = false) {
let polite = (parseInt(peerID.charAt(peerID.length - 1), 16) % 2 == 0) && !remote;
this.setPolite(polite);
}
setupDataChannel() {
if (!this.dataChannel) {
throw new Error();
@@ -513,6 +516,7 @@ class PeerConnection {
}
try {
this.makingOffer = true;
this.setPoliteFromID(this.peerManager.peerID);
await this.rtcPeer.setLocalDescription();
if (!this.rtcPeer.localDescription) {
return;
@@ -590,8 +594,6 @@ class PeerConnection {
catch (err) {
console.error(err);
}
// };
// */
}
disconnect() {
this.rtcPeer?.close();
@@ -651,10 +653,10 @@ class PeerConnection {
}
call(functionName, args) {
let transactionID = generateID(); // make this faster as we will only ever have a small number of in-flight queries on a peer
// Think about a timeout here to auto reject it after a while.
let promise = new Promise((resolve, reject) => {
this.pendingRPCs.set(transactionID, { resolve, reject, functionName });
setTimeout(() => reject(`function:${functionName}[${transactionID}] failed to resolve after 10 seconds.`), 10000);
let timeoutSeconds = 10;
setTimeout(() => reject(`function:${functionName}[${transactionID}] failed to resolve after ${timeoutSeconds} seconds.`), timeoutSeconds * 1000);
});
let message = {
type: "rpc_call",