Angular RXJS在Websocket断开后重新连接侦听器
我将RXJS与Angular一起使用,并且有一个websocket服务,它是RXJS WebSocketSubject上的一个简单包装器 我还有一些服务需要订阅此服务以获取数据。在这些服务的初始阶段,它们订阅“基本”websocket 在基本websocket与服务器断开连接之前,这一切都可以正常工作。它正确地重建了自身,因此我能够将数据推送到服务器。但是,我的其他服务的订阅没有重新应用,因此我无法接收任何数据 建议采用什么方法解决此问题?谢谢 基础腹板箱(简化):Angular RXJS在Websocket断开后重新连接侦听器,angular,websocket,rxjs,Angular,Websocket,Rxjs,我将RXJS与Angular一起使用,并且有一个websocket服务,它是RXJS WebSocketSubject上的一个简单包装器 我还有一些服务需要订阅此服务以获取数据。在这些服务的初始阶段,它们订阅“基本”websocket 在基本websocket与服务器断开连接之前,这一切都可以正常工作。它正确地重建了自身,因此我能够将数据推送到服务器。但是,我的其他服务的订阅没有重新应用,因此我无法接收任何数据 建议采用什么方法解决此问题?谢谢 基础腹板箱(简化): 从'@angular/cor
从'@angular/core'导入{Injectable};
从'rxjs/webSocket'导入{WebSocketSubject,WebSocketSubjectConfig};
从“/auth-service”导入{auth-service};
从“/url服务”导入{UrlService};
@注射的({
providedIn:'根'
})
导出类AppWsService{
私有套接字!:WebSocketSubject;
构造函数(专用urlService:urlService){
这是初始化();
}
私有异步初始化(){
等待此消息。fetchWebsocket();
}
专用异步发送消息(消息:任意){
等待此消息。fetchWebsocket();
console.log(“[BASE]发送消息”,消息);
this.socket.next(消息);
}
公共异步fetchWebsocket():承诺{
if(!this.socket){
let config:WebSocketSubjectConfig={
url:this.urlService.get_ws_url(),
openObserver:{
下一步:((任意)=>{
让msg={
//在这里包括auth内容
};
this.socket.next(如有消息);
},
错误:(err:any)=>{
log(“[BASE][WS]连接时出错:”,错误);
}
}
};
console.log(“[BASE][WS]Making subject…”)
this.socket=新的WebSocketSubject(配置);
这是socket.subscribe({
下一步:(数据)=>{
调试('[BASE][WS]获取消息:',数据);
调试('[BASE][WS]类型:',data.constructor.name);
},
错误:(msg)=>{
console.error(“[BASE][WS]error:”,msg);
}
});
}
console.log('[BASE][WS]返回主题')
退回这个.socket;
}
}
使用基本websocket的示例服务(简化):
从'@angular/core'导入{Injectable};
从'rxjs/webSocket'导入{WebSocketSubject};
从'rxjs'导入{Subject};
从“/app ws service”导入{AppWsService};
从“/definitions/chat messages”导入{ChatInfo,isChatMessage,isChatInfo,isChatInfos};
@注射的({
providedIn:'根'
})
出口类聊天室服务{
私有ws!:WebSocketSubject;
构造函数(专用appWsService:appWsService){
this.initWebsocket();
}
私有异步initWebsocket(){
this.ws=等待this.appWsService.fetchWebsocket();
这是订阅({
下一步:(数据)=>{
if(isChatMessage(数据)){
//做事
}else if(isChatInfo(数据)){
//做事
}else if(iChatInfos(数据)){
//做事
}
}
});
}
}
import { Injectable } from '@angular/core';
import { WebSocketSubject, WebSocketSubjectConfig } from 'rxjs/webSocket';
import { AuthService } from './auth-service';
import { UrlService } from './url-service';
@Injectable({
providedIn: 'root'
})
export class AppWsService {
private socket!: WebSocketSubject<any>;
constructor(private urlService: UrlService) {
this.initialize();
}
private async initialize() {
await this.fetchWebsocket();
}
private async send_message(message: any) {
await this.fetchWebsocket();
console.log('[BASE] Send message', message);
this.socket.next(message);
}
public async fetchWebsocket(): Promise<WebSocketSubject<any>> {
if (!this.socket) {
let config: WebSocketSubjectConfig<any> = {
url: this.urlService.get_ws_url(),
openObserver: {
next: (_: any) => {
let msg = {
// Include auth stuff here
};
this.socket.next(msg as any);
},
error: (err: any) => {
console.log('[BASE][WS] Error connecting:', err);
}
}
};
console.log('[BASE][WS]Making subject...')
this.socket = new WebSocketSubject(config);
this.socket.subscribe({
next: (data) => {
console.debug('[BASE][WS] Got message:', data);
console.debug('[BASE][WS] Type:', data.constructor.name);
},
error: (msg) => {
console.error('[BASE][WS] Error:', msg);
}
});
}
console.log('[BASE][WS] Returning subject')
return this.socket;
}
}
import { Injectable } from '@angular/core';
import { WebSocketSubject } from 'rxjs/webSocket';
import { Subject } from 'rxjs';
import { AppWsService } from './app-ws-service';
import { ChatInfo, isChatMessage, isChatInfo, isChatInfos } from './definitions/chat-messages';
@Injectable({
providedIn: 'root'
})
export class ChatService {
private ws!: WebSocketSubject<any>;
constructor(private appWsService: AppWsService) {
this.initWebsocket();
}
private async initWebsocket() {
this.ws = await this.appWsService.fetchWebsocket();
this.ws.subscribe({
next: (data) => {
if (isChatMessage(data)) {
// Do stuff
} else if (isChatInfo(data)) {
// Do stuff
} else if (isChatInfos(data)) {
// Do stuff
}
}
});
}
}