mirror of
https://github.com/cerc-io/watcher-ts
synced 2025-01-07 20:08:06 +00:00
Fix for multiple connections between two peers (#305)
* Close existing connection * Fix closing existing connections * Close new connections from only peer * Refresh in intervals to update UI stats
This commit is contained in:
parent
f619c71b42
commit
a5658c6344
@ -263,11 +263,27 @@ export class Peer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async _handleConnect (connection: Connection): Promise<void> {
|
async _handleConnect (connection: Connection): Promise<void> {
|
||||||
|
assert(this._node);
|
||||||
const remotePeerId = connection.remotePeer;
|
const remotePeerId = connection.remotePeer;
|
||||||
|
const remoteConnections = this._node.getConnections(remotePeerId);
|
||||||
|
|
||||||
// Log connected peer
|
// Log connected peer
|
||||||
console.log(`Connected to ${remotePeerId.toString()} using multiaddr ${connection.remoteAddr.toString()}`);
|
console.log(`Connected to ${remotePeerId.toString()} using multiaddr ${connection.remoteAddr.toString()}`);
|
||||||
console.log(`Current number of peers connected: ${this._node?.getPeers().length}`);
|
|
||||||
|
// Keep only one connection with a peer.
|
||||||
|
if (remoteConnections.length > 1) {
|
||||||
|
// Close new connection from peer having the smaller peer id.
|
||||||
|
if (this._node.peerId.toString() < remotePeerId.toString()) {
|
||||||
|
console.log('Closing new connection for already connected peer');
|
||||||
|
// Close new connection as protocol stream is opened in the first connection that is established.
|
||||||
|
await connection.close();
|
||||||
|
console.log('Closed');
|
||||||
|
}
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log(`Current number of peers connected: ${this._node.getPeers().length}`);
|
||||||
|
|
||||||
// Start heartbeat check peer
|
// Start heartbeat check peer
|
||||||
await this._startHeartbeatChecks(
|
await this._startHeartbeatChecks(
|
||||||
|
Loading…
Reference in New Issue
Block a user