* Refactor handlePeer function to use async/await and emit "peer" event, and make Proxy class extend EventEmitter.
This commit is contained in:
parent
8b678e81e8
commit
1c9a430d5e
|
@ -70,14 +70,20 @@ const errorSocketEncoding = {
|
||||||
const nextSocketId = idFactory(1);
|
const nextSocketId = idFactory(1);
|
||||||
|
|
||||||
export default class MultiSocketProxy extends Proxy {
|
export default class MultiSocketProxy extends Proxy {
|
||||||
handlePeer({ peer, muxer, ...options }: DataSocketOptions & PeerOptions) {
|
async handlePeer({
|
||||||
new Peer({
|
peer,
|
||||||
|
muxer,
|
||||||
|
...options
|
||||||
|
}: DataSocketOptions & PeerOptions) {
|
||||||
|
const conn = new Peer({
|
||||||
...this.socketOptions,
|
...this.socketOptions,
|
||||||
proxy: this,
|
proxy: this,
|
||||||
peer,
|
peer,
|
||||||
muxer,
|
muxer,
|
||||||
...options,
|
...options,
|
||||||
}).init();
|
});
|
||||||
|
await conn.init();
|
||||||
|
this.emit("peer", conn);
|
||||||
}
|
}
|
||||||
private socketClass: any;
|
private socketClass: any;
|
||||||
private _peers: Map<string, PeerEntity> = new Map<string, PeerEntity>();
|
private _peers: Map<string, PeerEntity> = new Map<string, PeerEntity>();
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
import Protomux from "protomux";
|
import Protomux from "protomux";
|
||||||
import { DataSocketOptions, PeerOptions } from "./peer.js";
|
import { DataSocketOptions, PeerOptions } from "./peer.js";
|
||||||
|
import EventEmitter from "events";
|
||||||
|
|
||||||
export interface ProxyOptions extends DataSocketOptions {
|
export interface ProxyOptions extends DataSocketOptions {
|
||||||
swarm: any;
|
swarm: any;
|
||||||
|
@ -8,7 +9,7 @@ export interface ProxyOptions extends DataSocketOptions {
|
||||||
autostart?: boolean;
|
autostart?: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
export default abstract class Proxy {
|
export default abstract class Proxy extends EventEmitter {
|
||||||
protected _listen: any;
|
protected _listen: any;
|
||||||
protected _autostart: boolean;
|
protected _autostart: boolean;
|
||||||
|
|
||||||
|
@ -24,6 +25,7 @@ export default abstract class Proxy {
|
||||||
autostart = false,
|
autostart = false,
|
||||||
emulateWebsocket = false,
|
emulateWebsocket = false,
|
||||||
}: ProxyOptions) {
|
}: ProxyOptions) {
|
||||||
|
super();
|
||||||
this._swarm = swarm;
|
this._swarm = swarm;
|
||||||
this._protocol = protocol;
|
this._protocol = protocol;
|
||||||
this._listen = listen;
|
this._listen = listen;
|
||||||
|
|
Loading…
Reference in New Issue