*Refactor bootstrap so for every peer we get, we will trigger a connection to it
*Move initial hello handshake to private method and remove us from the explicit peer list in case it was added via bootstrap
This commit is contained in:
parent
638df772fa
commit
2465b80441
28
src/index.ts
28
src/index.ts
|
@ -61,9 +61,7 @@ export default class DHTCache extends EventEmitter {
|
||||||
this.flood.on("peer-remove", (peer) => this.removePeerHandler(peer));
|
this.flood.on("peer-remove", (peer) => this.removePeerHandler(peer));
|
||||||
this.flood.on("message", (message, id) => this.onGetBroadcast(message, id));
|
this.flood.on("message", (message, id) => this.onGetBroadcast(message, id));
|
||||||
|
|
||||||
this.swarm.on("connection", (peer: any) =>
|
this.swarm.on("connection", this._hello.bind(this));
|
||||||
this.send(peer, b4a.from("hello"))
|
|
||||||
);
|
|
||||||
|
|
||||||
[...this.swarm.peers.values()]
|
[...this.swarm.peers.values()]
|
||||||
.map((item) => {
|
.map((item) => {
|
||||||
|
@ -398,25 +396,36 @@ export default class DHTCache extends EventEmitter {
|
||||||
}
|
}
|
||||||
|
|
||||||
private _bootstrapFrom(bootstrap: Bootstrap) {
|
private _bootstrapFrom(bootstrap: Bootstrap) {
|
||||||
if (this.bootstrapped) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (const id in bootstrap) {
|
for (const id in bootstrap) {
|
||||||
const { connectedTo } = bootstrap[id];
|
const { connectedTo } = bootstrap[id];
|
||||||
if (id === this.id.toString("hex")) {
|
if (id === this.id.toString("hex")) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!this.connectedTo.has(id)) {
|
||||||
|
this.swarm.joinPeer(id);
|
||||||
|
}
|
||||||
|
|
||||||
for (const connection of connectedTo) {
|
for (const connection of connectedTo) {
|
||||||
const peer = b4a.from(connection) as Buffer;
|
const peer = b4a.from(connection) as Buffer;
|
||||||
|
|
||||||
|
if (b4a.equals(peer, this.id)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
this._ensurePeer(peer);
|
this._ensurePeer(peer);
|
||||||
this._addEntityConnection(id, peer);
|
this._addEntityConnection(id, peer);
|
||||||
|
|
||||||
|
if (!this.connectedTo.has(peer.toString("hex"))) {
|
||||||
|
this.swarm.joinPeer(peer);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!this.bootstrapped) {
|
||||||
this.bootstrapped = true;
|
this.bootstrapped = true;
|
||||||
this.emit("bootstrapped");
|
this.emit("bootstrapped");
|
||||||
|
}
|
||||||
|
|
||||||
this._recalculate();
|
this._recalculate();
|
||||||
}
|
}
|
||||||
|
@ -574,4 +583,9 @@ export default class DHTCache extends EventEmitter {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private _hello(peer: any) {
|
||||||
|
this.send(peer, b4a.from("hello"));
|
||||||
|
this.swarm.leavePeer(peer.remotePublicKey);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue