working websocket and bootstrap peer reconnection when connection is lost

This commit is contained in:
2025-05-18 15:11:34 -07:00
parent 404a224bf1
commit 15e595cca1
6 changed files with 257 additions and 93 deletions

View File

@@ -29,6 +29,7 @@ Restruucture the app around the data. App/WS split is messy. Clean it up.
import { openDatabase, getData, addData, deleteData, mergeDataArray, getAllData, checkPostIds, getAllIds, getPostsByIds } from "db";
import { generateID } from "IDUtils";
import { PeerManager, PeerEventTypes } from "PeerManager";
import { Sync } from "Sync";
import { log, logID, renderLog, setLogVisibility } from "log";
// let posts:any;
// let keyBase = "dandelion_posts_v1_"
@@ -584,16 +585,39 @@ class App {
// we could return progress information as we connect and have the app subscribe to that?
// Would be lovely to show a little display of peers connecting, whether you're connected directly to a friend's peer etc.
// Basically that live "dandelion" display.
this.peerManager.registerRPC('getPeersForUser', (userID) => {
return [1, 2, 3, 4, 5];
});
this.peerManager.registerRPC('getPostIDsForUser', (userID) => {
return [1, 2, 3, 4, 5];
});
await this.peerManager.connect();
console.log.apply(null, log("*************** after peerManager.connect"));
;
if (!this.isBootstrapPeer) {
let postIDs = await this.peerManager.rpc.getPostIDsForUser(this.peerManager.bootstrapPeerID, this.userID);
console.log.apply(null, log("peerManager.rpc.getPostIDsForUser", postIDs));
if (this.isBootstrapPeer) {
return;
}
let usersToSync = await Sync.getFollowing(this.userID);
for (let userID of usersToSync) {
console.log(userID);
// this.peerManager.rpc.getPeersForUser(userID);
}
// for (let userID in this.sync.usersToSync()) {
// let peers = await this.peerManager.rpc.getPeersForUser(userID);
// for (let peer in peers) {
// let peer = await this.peerManager.connectToPeer(userID);
// let postIDs = peer.getPostIDsForUser(userID);
// let postIDsNeeded = this.sync.checkPostIds(userID, postIDs);
// if (postIDs.length === 0) {
// continue;
// }
// let posts = peer.rpc.getPostsForUser(userID, postIDs);
// this.sync.writePostsForUser(userID, posts);
// this.render();
// }
// }
let postIDs = await this.peerManager.rpc.getPostIDsForUser(this.peerManager.bootstrapPeerID, this.userID);
console.log.apply(null, log("peerManager.rpc.getPostIDsForUser", postIDs));
}
getPreferentialUserID() {
return this.router.userID.length !== 0 ? this.router.userID : this.userID;
@@ -1175,10 +1199,10 @@ class App {
this.peerManager.registerRPC('ping', (args) => {
return { id: this.peerID, user: this.userID, user_name: this.username, peer_name: this.peername };
});
if (!this.isBootstrapPeer) {
let pong = await this.peerManager.rpc.ping(this.peerManager.bootstrapPeerID);
console.log.apply(null, log('pong from: ', pong));
}
// if (!this.isBootstrapPeer) {
// let pong = await this.peerManager.rpc.ping(this.peerManager.bootstrapPeerID);
// console.log.apply(null, log('pong from: ', pong));
// }
// this.peerManager.registerRPC('getPostIDsForUser', (args: any) => {
// this.sync.getPostsForUser
// });
@@ -1187,11 +1211,14 @@ class App {
if (!this.peerManager) {
throw new Error();
}
this.peerManager.registerRPC('getPostIDsForUser', (args) => {
this.peerManager.registerRPC('getPeersForUser', (userID) => {
return [1, 2, 3, 4, 5];
});
let postIDs = await this.peerManager.rpc.getPostIDsForUser("dummy_peer", "bloop");
console.log.apply(null, log("peerManager.rpc.getPostIDsForUser", postIDs));
// this.peerManager.registerRPC('getPostIDsForUser', (args: any) => {
// return [1, 2, 3, 4, 5];
// });
// let postIDs = await this.peerManager.rpc.getPostIDsForUser("dummy_peer", "bloop");
// console.log.apply(null, log("peerManager.rpc.getPostIDsForUser", postIDs));
// this.peerManager.registerSearchQuery('find_peers_for_user', this.query_findPeersForUser);
// let peers = await this.peerManager.search('find_peers_for_user', { 'user_id': 'bloop' });
}