working websocket and bootstrap peer reconnection when connection is lost
This commit is contained in:
68
src/main2.ts
68
src/main2.ts
@@ -34,6 +34,7 @@ Restruucture the app around the data. App/WS split is messy. Clean it up.
|
||||
import { openDatabase, getData, addData, addDataArray, clearData, 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"
|
||||
|
||||
@@ -748,7 +749,7 @@ class App {
|
||||
console.log.apply(null, log(`[app]: peer connected:${event.peerID}`));
|
||||
// rpc saying what peers we have
|
||||
})
|
||||
|
||||
|
||||
console.log.apply(null, log("*************** before peerManager.connect"));
|
||||
|
||||
// We use promises here to only return from this call once we're connected to the boostrap peer
|
||||
@@ -759,6 +760,11 @@ class App {
|
||||
// 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: any) => {
|
||||
return [1, 2, 3, 4, 5];
|
||||
});
|
||||
|
||||
|
||||
this.peerManager.registerRPC('getPostIDsForUser', (userID: any) => {
|
||||
return [1, 2, 3, 4, 5]
|
||||
});
|
||||
@@ -767,12 +773,45 @@ class App {
|
||||
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));
|
||||
|
||||
}
|
||||
|
||||
@@ -1538,10 +1577,10 @@ class App {
|
||||
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));
|
||||
// }
|
||||
|
||||
|
||||
|
||||
@@ -1557,13 +1596,18 @@ class App {
|
||||
throw new Error();
|
||||
}
|
||||
|
||||
this.peerManager.registerRPC('getPostIDsForUser', (args: any) => {
|
||||
return [1, 2, 3, 4, 5]
|
||||
this.peerManager.registerRPC('getPeersForUser', (userID: 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.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);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user