mirror of
https://github.com/cerc-io/watcher-ts
synced 2025-01-09 12:58:06 +00:00
Refactor protocol stream creation
This commit is contained in:
parent
759c593b14
commit
87aed42d50
@ -258,8 +258,7 @@ export class Peer {
|
|||||||
|
|
||||||
// Open stream if connection exists and it doesn't already have a stream with chat protocol
|
// Open stream if connection exists and it doesn't already have a stream with chat protocol
|
||||||
if (connection && !connection.streams.some(stream => stream.stat.protocol === CHAT_PROTOCOL)) {
|
if (connection && !connection.streams.some(stream => stream.stat.protocol === CHAT_PROTOCOL)) {
|
||||||
const stream = await connection.newStream([CHAT_PROTOCOL]);
|
await this._createProtocolStream(connection, CHAT_PROTOCOL);
|
||||||
this._handleStream(peerId, stream);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -329,18 +328,12 @@ export class Peer {
|
|||||||
console.log('Closed');
|
console.log('Closed');
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
// Open stream in new connection for chat protocol (if handled by remote peer)
|
||||||
// Open stream in new connection for chat protocol
|
|
||||||
const protocols = await this._node.peerStore.protoBook.get(remotePeerId);
|
const protocols = await this._node.peerStore.protoBook.get(remotePeerId);
|
||||||
|
|
||||||
// Dial if protocol is handled by remote peer
|
|
||||||
// The chat protocol may not be updated in the list and will be handled later on change:protocols event
|
// The chat protocol may not be updated in the list and will be handled later on change:protocols event
|
||||||
if (protocols.includes(CHAT_PROTOCOL)) {
|
if (protocols.includes(CHAT_PROTOCOL)) {
|
||||||
const stream = await connection.newStream([CHAT_PROTOCOL]);
|
await this._createProtocolStream(connection, CHAT_PROTOCOL);
|
||||||
this._handleStream(remotePeerId, stream);
|
|
||||||
}
|
|
||||||
} catch (err: any) {
|
|
||||||
console.log(`Could not create a new protocol stream with ${remotePeerId.toString()}`, err);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -353,6 +346,18 @@ export class Peer {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async _createProtocolStream (connection: Connection, protocol: string) {
|
||||||
|
assert(this._node);
|
||||||
|
const remotePeerId = connection.remotePeer;
|
||||||
|
|
||||||
|
try {
|
||||||
|
const stream = await connection.newStream([protocol]);
|
||||||
|
this._handleStream(remotePeerId, stream);
|
||||||
|
} catch (err: any) {
|
||||||
|
console.log(`Could not create a new ${protocol} stream with ${remotePeerId.toString()}`, err);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
async _handleDeadConnections (remotePeerId: PeerId) {
|
async _handleDeadConnections (remotePeerId: PeerId) {
|
||||||
// Close existing connections of remote peer
|
// Close existing connections of remote peer
|
||||||
console.log(`Closing connections for ${remotePeerId}`);
|
console.log(`Closing connections for ${remotePeerId}`);
|
||||||
|
Loading…
Reference in New Issue
Block a user