refactor: add end method to transports

This commit is contained in:
Derrick Hammer 2023-09-01 20:55:08 -04:00
parent e821da5a27
commit 90c0614773
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
4 changed files with 11 additions and 0 deletions

View File

@ -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;
} }

View File

@ -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;

View File

@ -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) => {

View File

@ -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[] };