“OperatorFunction<Obs​​ervableInput<unknown>, unknown>”类型的参数不可分配给“OperatorFunction<unknown, unknown>”类型的参数

时间:2021-07-12 12:04:37

标签: angular rxjs

尝试在 Angular 应用程序中实现 websocket。但我收到此错误

<块引用>

“OperatorFunction”类型的参数不能分配给“OperatorFunction”类型的参数

无法前进。在 rxjs => switchAll() 中出现错误。

service.ts 文件:

import { Injectable } from '@angular/core';
import { webSocket, WebSocketSubject } from 'rxjs/webSocket';
import { environment } from '../environments/environment';
import { catchError, switchAll, tap } from 'rxjs/operators';
import { EMPTY, Subject } from 'rxjs';
export const WS_ENDPOINT = environment.socketbaseUrl;
  
@Injectable({
  providedIn: 'root'
})
export class DataService {
  private socket: WebSocketSubject<any>;
  private messagesSubject = new Subject();
  public messages = this.messagesSubject.pipe(switchAll(), catchError(e => { throw e }));
  
  public connect(): void {
  
    if (!this.socket || this.socket.closed) {
      this.socket = this.getNewWebSocket();
      const messages = this.socket.pipe(
        tap({
          error: error => console.log(error),
        }), catchError(_ => EMPTY));
      this.messagesSubject.next(messages);
    }
}
  
    private getNewWebSocket() {
       return webSocket(WS_ENDPOINT);
    }
    sendMessage(msg: any) {
       this.socket.next(msg);
    }
    close() {
      this.socket.complete(); 
    }
}

0 个答案:

没有答案