尝试在 Angular 应用程序中实现 websocket。但我收到此错误
<块引用>“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();
}
}