refactor: add end method to transports
This commit is contained in:
parent
e821da5a27
commit
90c0614773
|
@ -38,4 +38,6 @@ export abstract class BasePeer implements Peer {
|
||||||
logger,
|
logger,
|
||||||
}: { onDone?: any; onError?: (...args: any[]) => void; logger: Logger },
|
}: { onDone?: any; onError?: (...args: any[]) => void; logger: Logger },
|
||||||
): void;
|
): void;
|
||||||
|
|
||||||
|
abstract end(): void;
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,6 +6,8 @@ import * as console from "console";
|
||||||
import { BasePeer } from "#transports/base.js";
|
import { BasePeer } from "#transports/base.js";
|
||||||
|
|
||||||
export class TcpPeer extends BasePeer implements Peer {
|
export class TcpPeer extends BasePeer implements Peer {
|
||||||
|
declare _socket: net.Socket;
|
||||||
|
|
||||||
sendMessage(message: Uint8Array): void {
|
sendMessage(message: Uint8Array): void {
|
||||||
this._socket.write(message);
|
this._socket.write(message);
|
||||||
}
|
}
|
||||||
|
@ -59,6 +61,9 @@ export class TcpPeer extends BasePeer implements Peer {
|
||||||
this._socket.on("error", onError);
|
this._socket.on("error", onError);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
end(): void {
|
||||||
|
this._socket.end();
|
||||||
|
}
|
||||||
|
|
||||||
public static async connect(uri: URL): Promise<net.Socket> {
|
public static async connect(uri: URL): Promise<net.Socket> {
|
||||||
const host = uri.hostname;
|
const host = uri.hostname;
|
||||||
|
|
|
@ -42,6 +42,9 @@ export class WebSocketPeer extends BasePeer implements Peer {
|
||||||
this._socket.addEventListener("error", onError);
|
this._socket.addEventListener("error", onError);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
end(): void {
|
||||||
|
this._socket.end();
|
||||||
|
}
|
||||||
|
|
||||||
public static async connect(uri: URL): Promise<WebSocket> {
|
public static async connect(uri: URL): Promise<WebSocket> {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
|
|
|
@ -28,6 +28,7 @@ export interface Peer {
|
||||||
): void;
|
): void;
|
||||||
|
|
||||||
renderLocationUri(): string;
|
renderLocationUri(): string;
|
||||||
|
end(): void;
|
||||||
}
|
}
|
||||||
|
|
||||||
export type PeerConstructorOptions = { socket: any; uris?: URL[] };
|
export type PeerConstructorOptions = { socket: any; uris?: URL[] };
|
||||||
|
|
Loading…
Reference in New Issue