Announce users the local peers knows to remote peers when they connect. Fix RPC system to correctly serialize and multiple params. Add detailed logging for rpc calls.
This commit is contained in:
46
src/main2.ts
46
src/main2.ts
@@ -740,15 +740,29 @@ class App {
|
||||
connectURL: string = "";
|
||||
firstRun = false;
|
||||
peerManager: PeerManager | null = null;
|
||||
sync = new Sync();
|
||||
|
||||
async connect() {
|
||||
this.peerManager = new PeerManager(this.userID, this.peerID, this.isBootstrapPeer);
|
||||
if (this.peerManager === null) {
|
||||
throw new Error();
|
||||
}
|
||||
this.registerRPCs();
|
||||
|
||||
this.peerManager.addEventListener(PeerEventTypes.PEER_CONNECTED, (event: any) => {
|
||||
this.peerManager.addEventListener(PeerEventTypes.PEER_CONNECTED, async (event: any) => {
|
||||
if (!this.peerManager) {
|
||||
throw new Error();
|
||||
}
|
||||
console.log.apply(null, log(`[app]: peer connected:${event.peerID}`));
|
||||
let knownUsers = await this.sync.getKnownUsers();
|
||||
this.peerManager.rpc.announceUsers(event.peerID, this.peerID, knownUsers);
|
||||
// rpc saying what peers we have
|
||||
})
|
||||
});
|
||||
|
||||
this.peerManager.addEventListener(PeerEventTypes.PEER_DISCONNECTED, async (event: any) => {
|
||||
console.log.apply(null, log(`[app]: peer disconnected:${event.peerID}`));
|
||||
});
|
||||
|
||||
|
||||
console.log.apply(null, log("*************** before peerManager.connect"));
|
||||
|
||||
@@ -760,13 +774,23 @@ 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('announceUsers', (sendingPeerID: string, userIDs: string[]) => {
|
||||
console.log.apply(null, log(`announceUsers from ${sendingPeerID}`, userIDs));
|
||||
|
||||
for (let userID of userIDs) {
|
||||
console.log.apply(null, log(`[app] announceUsers, got user:${userID} from peer ${sendingPeerID}`));
|
||||
}
|
||||
});
|
||||
|
||||
this.peerManager.registerRPC('getPeersForUser', (userID: any) => {
|
||||
return [1, 2, 3, 4, 5];
|
||||
});
|
||||
|
||||
|
||||
this.peerManager.registerRPC('getPostIDsForUser', (userID: any) => {
|
||||
return [1, 2, 3, 4, 5]
|
||||
let postIDs = this.sync.getPostIdsForUser(userID);
|
||||
|
||||
return postIDs;
|
||||
});
|
||||
|
||||
await this.peerManager.connect();
|
||||
@@ -777,12 +801,12 @@ class App {
|
||||
return;
|
||||
}
|
||||
|
||||
let usersToSync = await Sync.getFollowing(this.userID);
|
||||
// let usersToSync = await Sync.getFollowing(this.userID);
|
||||
|
||||
for (let userID of usersToSync) {
|
||||
console.log(userID);
|
||||
// this.peerManager.rpc.getPeersForUser(userID);
|
||||
}
|
||||
// for (let userID of usersToSync) {
|
||||
// console.log(userID);
|
||||
// // this.peerManager.rpc.getPeersForUser(userID);
|
||||
// }
|
||||
|
||||
|
||||
// for (let userID in this.sync.usersToSync()) {
|
||||
@@ -808,10 +832,10 @@ class App {
|
||||
|
||||
// }
|
||||
|
||||
let postIDs = await this.peerManager.rpc.getPostIDsForUser(this.peerManager.bootstrapPeerID, this.userID);
|
||||
// let postIDs = await this.peerManager.rpc.getPostIDsForUser(this.peerManager.bootstrapPeerID, this.userID);
|
||||
|
||||
|
||||
console.log.apply(null, log("peerManager.rpc.getPostIDsForUser", postIDs));
|
||||
// console.log.apply(null, log("peerManager.rpc.getPostIDsForUser", postIDs));
|
||||
|
||||
}
|
||||
|
||||
@@ -1635,6 +1659,8 @@ class App {
|
||||
this.userID = this.getUserID();
|
||||
this.username = this.getUsername();
|
||||
|
||||
this.sync.setUserID(this.userID);
|
||||
|
||||
this.connect();
|
||||
|
||||
// this.registerRPCs();
|
||||
|
||||
Reference in New Issue
Block a user