working datachannel and inital RPC test
This commit is contained in:
93
src/main2.ts
93
src/main2.ts
@@ -35,6 +35,8 @@ import { openDatabase, getData, addData, addDataArray, clearData, deleteData, me
|
||||
import { generateID } from "IDUtils";
|
||||
import { PeerManager } from "PeerManager";
|
||||
|
||||
import {log, renderLog, setLogVisibility} from "log"
|
||||
|
||||
// import {PeerConnection} from "webRTC";
|
||||
|
||||
// declare let WebTorrent: any;
|
||||
@@ -122,29 +124,7 @@ function logID(ID: string) {
|
||||
|
||||
// }
|
||||
|
||||
let logLines: string[] = [];
|
||||
let logLength = 30;
|
||||
let logVisible = false;
|
||||
function renderLog() {
|
||||
if (!logVisible) {
|
||||
return;
|
||||
}
|
||||
|
||||
let log = document.getElementById("log");
|
||||
if (!log) {
|
||||
throw new Error();
|
||||
}
|
||||
log.innerText = logLines.join("\n");
|
||||
}
|
||||
function log(message: string) {
|
||||
console.log(message);
|
||||
logLines.push(`${new Date().toLocaleTimeString()}: ${message}`);
|
||||
if (logLines.length > logLength) {
|
||||
logLines = logLines.slice(logLines.length - logLength);
|
||||
}
|
||||
|
||||
renderLog();
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -839,6 +819,36 @@ class App {
|
||||
firstRun = false;
|
||||
peerManager: PeerManager | null = null;
|
||||
|
||||
async connect() {
|
||||
this.peerManager = new PeerManager(this.userID, this.peerID, this.isBootstrapPeer);
|
||||
log("*************** before peerManager.connect");
|
||||
|
||||
// We use promises here to only return from this call once we're connected to the boostrap peer
|
||||
// and the datachannel is open.
|
||||
// Might want to take this a step further and only return once we're connected to an initial set of peers?
|
||||
// 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('getPostIDsForUser', (userID: any) => {
|
||||
return [1, 2, 3, 4, 5]
|
||||
});
|
||||
|
||||
await this.peerManager.connect();
|
||||
log("*************** after peerManager.connect");
|
||||
|
||||
|
||||
if (!this.isBootstrapPeer) {
|
||||
let postIDs = await this.peerManager.rpc.getPostIDsForUser(this.peerManager.bootstrapPeerID, this.userID);
|
||||
console.log("peerManager.rpc.getPostIDsForUser", postIDs);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
getPreferentialUserID() {
|
||||
return this.router.userID.length !== 0 ? this.router.userID : this.userID;
|
||||
}
|
||||
@@ -1338,7 +1348,7 @@ class App {
|
||||
return;
|
||||
}
|
||||
infoElement.style.display == 'none' ? infoElement.style.display = 'block' : infoElement.style.display = 'none';
|
||||
logVisible = infoElement.style.display == 'block';
|
||||
setLogVisibility(infoElement.style.display == 'block');
|
||||
renderLog();
|
||||
this.lazyCreateQRCode();
|
||||
(document.querySelector('#qrcode > img') as HTMLImageElement).classList.add('qrcode_image');
|
||||
@@ -1592,6 +1602,29 @@ class App {
|
||||
return false;
|
||||
}
|
||||
|
||||
async registerRPCs() {
|
||||
if (!this.peerManager) {
|
||||
throw new Error();
|
||||
}
|
||||
|
||||
this.peerManager.registerRPC('ping', (args: any) => {
|
||||
return {id:this.peerID};
|
||||
});
|
||||
|
||||
if (!this.isBootstrapPeer) {
|
||||
let pong = await this.peerManager.rpc.ping(this.peerManager.bootstrapPeerID);
|
||||
console.log(pong);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
// this.peerManager.registerRPC('getPostIDsForUser', (args: any) => {
|
||||
// this.sync.getPostsForUser
|
||||
// });
|
||||
|
||||
}
|
||||
|
||||
async testPeerManager() {
|
||||
if (!this.peerManager) {
|
||||
throw new Error();
|
||||
@@ -1631,8 +1664,10 @@ class App {
|
||||
this.userID = this.getUserID();
|
||||
this.username = this.getUsername();
|
||||
|
||||
this.peerManager = new PeerManager(this.userID, this.peerID, this.isBootstrapPeer);
|
||||
this.testPeerManager();
|
||||
this.connect();
|
||||
|
||||
this.registerRPCs();
|
||||
// this.testPeerManager();
|
||||
|
||||
// let peer: RTCPeerConnection | null = null;
|
||||
// // if (window.RTCPeerConnection) {
|
||||
@@ -2105,6 +2140,14 @@ namespace App {
|
||||
HOME,
|
||||
CONNECT,
|
||||
};
|
||||
|
||||
// export function connect() {
|
||||
// throw new Error("Function not implemented.");
|
||||
// }
|
||||
|
||||
// export function connect() {
|
||||
// throw new Error("Function not implemented.");
|
||||
// }
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user