@nestjs/common#WebSocketAdapter TypeScript Examples

The following examples show how to use @nestjs/common#WebSocketAdapter. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: application-config.d.ts    From nest-jaeger with MIT License 5 votes vote down vote up
setIoAdapter(ioAdapter: WebSocketAdapter): void;
Example #2
Source File: application-config.d.ts    From nest-jaeger with MIT License 5 votes vote down vote up
getIoAdapter(): WebSocketAdapter;
Example #3
Source File: application-config.d.ts    From nest-jaeger with MIT License 5 votes vote down vote up
constructor(ioAdapter?: WebSocketAdapter | null);
Example #4
Source File: nest-application.d.ts    From nest-jaeger with MIT License 5 votes vote down vote up
useWebSocketAdapter(adapter: WebSocketAdapter): this;
Example #5
Source File: UWebSocketAdapter.ts    From nestjs-uws with MIT License 5 votes vote down vote up
export class UWebSocketAdapter implements WebSocketAdapter {
  private instance: UWS.TemplatedApp = null;
  private listenSocket: string = null;
  private port: number = 0;

  constructor(args: ICreateServerArgs | ICreateServerSecureArgs) {
    this.port = args.port;
    // @ts-ignore
    if (args.sslKey) {
      this.instance = UWSBuilder.buildSSLApp(args as ICreateServerSecureArgs);
    } else {
      this.instance = UWSBuilder.buildApp(args);
    }
  }

  bindClientConnect(server: UWS.TemplatedApp, callback: Function): any {
    this.instance.ws('/*', {
      open: (socket) => {
        Object.defineProperty(socket, 'emitter', {
          configurable: false,
          value: new events.EventEmitter(),
        });
        callback(socket);
      },
      message: (socket, message, isBinary) => {
        socket['emitter'].emit('message', { message, isBinary });
      },
    }).any('/*', (res, req) => {
      res.end('Nothing to see here!');
    });
  }

  bindMessageHandlers(client: UWS.WebSocket, handlers: MessageMappingProperties[], process: (data: any) => Observable<any>): any {
    fromEvent(client['emitter'], 'message')
      .pipe(
        mergeMap((data: { message: ArrayBuffer, isBinary: boolean }) => this.bindMessageHandler(data, handlers, process)),
        filter(result => result),
      )
      .subscribe(response => client.send(JSON.stringify(response)));
  }

  bindMessageHandler(
    buffer: { message: ArrayBuffer, isBinary: boolean },
    handlers: MessageMappingProperties[],
    process: (data: any) => Observable<any>,
  ): Observable<any> {
    const stringMessageData = Buffer.from(buffer.message).toString('utf-8');
    const message = JSON.parse(stringMessageData);
    const messageHandler = handlers.find(
      handler => handler.message === message.event,
    );
    if (!messageHandler) {
      return EMPTY;
    }

    return process(messageHandler.callback(message.data));
  }

  close(): any {
    UWS.us_listen_socket_close(this.listenSocket);
    this.instance = null;
  }

  async create(): Promise<UWS.TemplatedApp> {
    return new Promise((resolve, reject) => this.instance.listen(this.port, (token) => {
      if (token) {
        this.listenSocket = token;
        resolve(this.instance);
      } else {
        reject('Can\'t start listening...');
      }
    }));
  }
}