mirror of
https://github.com/cerc-io/watcher-ts
synced 2025-01-09 04:48:05 +00:00
Refactor Peer constructor
This commit is contained in:
parent
87aed42d50
commit
3d8451feca
@ -13,7 +13,7 @@ import { pushable, Pushable } from 'it-pushable';
|
|||||||
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string';
|
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string';
|
||||||
import { toString as uint8ArrayToString } from 'uint8arrays/to-string';
|
import { toString as uint8ArrayToString } from 'uint8arrays/to-string';
|
||||||
|
|
||||||
import { webRTCDirect, WebRTCDirectComponents, P2P_WEBRTC_STAR_ID, WebRTCDirectNodeType } from '@cerc-io/webrtc-direct';
|
import { webRTCDirect, WebRTCDirectComponents, P2P_WEBRTC_STAR_ID, WebRTCDirectNodeType, WebRTCDirectInit } from '@cerc-io/webrtc-direct';
|
||||||
import { noise } from '@chainsafe/libp2p-noise';
|
import { noise } from '@chainsafe/libp2p-noise';
|
||||||
import { mplex } from '@libp2p/mplex';
|
import { mplex } from '@libp2p/mplex';
|
||||||
import type { Transport } from '@libp2p/interface-transport';
|
import type { Transport } from '@libp2p/interface-transport';
|
||||||
@ -48,17 +48,13 @@ export class Peer {
|
|||||||
const relayPeerId = this._relayNodeMultiaddr.getPeerId();
|
const relayPeerId = this._relayNodeMultiaddr.getPeerId();
|
||||||
assert(relayPeerId);
|
assert(relayPeerId);
|
||||||
|
|
||||||
// Instantiation in nodejs.
|
const initOptions: WebRTCDirectInit = {
|
||||||
if (nodejs) {
|
wrtc: nodejs ? wrtc : undefined, // Instantiation in nodejs
|
||||||
this._wrtcTransport = webRTCDirect({
|
enableSignalling: true,
|
||||||
wrtc,
|
nodeType: WebRTCDirectNodeType.Peer,
|
||||||
enableSignalling: true,
|
relayPeerId
|
||||||
nodeType: WebRTCDirectNodeType.Peer,
|
};
|
||||||
relayPeerId
|
this._wrtcTransport = webRTCDirect(initOptions);
|
||||||
});
|
|
||||||
} else {
|
|
||||||
this._wrtcTransport = webRTCDirect({ relayPeerId, enableSignalling: true });
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
get peerId (): PeerId | undefined {
|
get peerId (): PeerId | undefined {
|
||||||
@ -161,7 +157,7 @@ export class Peer {
|
|||||||
|
|
||||||
// Listen for pubsub messages
|
// Listen for pubsub messages
|
||||||
this._node.pubsub.addEventListener('message', (evt) => {
|
this._node.pubsub.addEventListener('message', (evt) => {
|
||||||
this._handleMessage(evt.detail);
|
this._handlePubSubMessage(evt.detail);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -304,7 +300,8 @@ export class Peer {
|
|||||||
// 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()}`);
|
||||||
|
|
||||||
// Manage connections and stream if peer id is smaller to break symmetry
|
// Manage connections and streams
|
||||||
|
// Check if peer id is smaller to break symmetry
|
||||||
if (this._node.peerId.toString() < remotePeerId.toString()) {
|
if (this._node.peerId.toString() < remotePeerId.toString()) {
|
||||||
const remoteConnections = this._node.getConnections(remotePeerId);
|
const remoteConnections = this._node.getConnections(remotePeerId);
|
||||||
|
|
||||||
@ -443,7 +440,7 @@ export class Peer {
|
|||||||
this._peerStreamMap.set(peerId.toString(), messageStream);
|
this._peerStreamMap.set(peerId.toString(), messageStream);
|
||||||
}
|
}
|
||||||
|
|
||||||
_handleMessage (msg: Message): void {
|
_handlePubSubMessage (msg: Message): void {
|
||||||
// Messages should be signed since globalSignaturePolicy is set to 'StrictSign'
|
// Messages should be signed since globalSignaturePolicy is set to 'StrictSign'
|
||||||
assert(msg.type === 'signed');
|
assert(msg.type === 'signed');
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user