websocket - 如何在网络工作者中以角度使用 RxJS websocket
问题描述
我已经在我的项目中实现了 websocket,但是浏览器性能非常慢,有时它也会冻结浏览器/浏览器选项卡。所以考虑在 web worker 中使用 websocket。如何在我的项目中将以下代码与 web worker 一起使用。
**.service.ts**
connect(): Observable<any> {
return of(wsURL).pipe(
map(apiUrl => apiUrl.replace(/^https/, 'wss') + '/stream'),
switchMap(wsUrl => {
if (this.connection$) {
return this.connection$;
} else {
this.connection$ = webSocket(wsUrl);
return this.connection$;
}
})
);
closeConnection() {
if (this.connection$) {
this.connection$.complete();
this.connection$ = null;
}
}
**component.ts**
ngOnInit() {
this.webSocket.connect().pipe(
takeUntil(this.destroyed$)
).subscribe(messages => this.messages.push(messages));
}
ngOnDestroy() {
this.destroyed$.next();
}
解决方案
推荐阅读
- javascript - Javascript /在开始时静音视频声音
- javascript - Javascript Promises 语法差异?
- algorithm - Algorithm to organise calendar events using minimum positions
- java - Structuring a class with optional parameters
- ios - WebView 不加载
- json - 无法在云源模板中测试 AWS DMS 端点
- r - 在 R 中的循环内有条件:不识别循环对象 (i) 并抛出错误:没有名为“i”的对象
- matrix - umfpack:发生错误:数字因式分解:内存不足
- wordpress - 按钮上的 Wordpress 文本颜色
- android - 从外部库获取字符串资源