-
-
Notifications
You must be signed in to change notification settings - Fork 7.4k
/
ws-adapter.ts
51 lines (43 loc) 路 1.65 KB
/
ws-adapter.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import { INestApplicationContext, WebSocketAdapter } from '@nestjs/common';
import { WsMessageHandler } from '@nestjs/common/interfaces';
import { isFunction } from '@nestjs/common/utils/shared.utils';
import { NestApplication } from '@nestjs/core';
import { Observable } from 'rxjs';
import { CONNECTION_EVENT, DISCONNECT_EVENT } from '../constants';
export interface BaseWsInstance {
on: (event: string, callback: Function) => void;
close: Function;
}
export abstract class AbstractWsAdapter<
TServer extends BaseWsInstance = any,
TClient extends BaseWsInstance = any,
TOptions = any,
> implements WebSocketAdapter<TServer, TClient, TOptions>
{
protected readonly httpServer: any;
constructor(appOrHttpServer?: INestApplicationContext | any) {
if (appOrHttpServer && appOrHttpServer instanceof NestApplication) {
this.httpServer = appOrHttpServer.getUnderlyingHttpServer();
} else {
this.httpServer = appOrHttpServer;
}
}
public bindClientConnect(server: TServer, callback: Function) {
server.on(CONNECTION_EVENT, callback);
}
public bindClientDisconnect(client: TClient, callback: Function) {
client.on(DISCONNECT_EVENT, callback);
}
public async close(server: TServer) {
const isCallable = server && isFunction(server.close);
isCallable && (await new Promise(resolve => server.close(resolve)));
}
// eslint-disable-next-line @typescript-eslint/no-empty-function
public async dispose() {}
public abstract create(port: number, options?: TOptions): TServer;
public abstract bindMessageHandlers(
client: TClient,
handlers: WsMessageHandler[],
transform: (data: any) => Observable<any>,
);
}