hermes-web-angular/src/app/hermes-socket.service.ts

53 lines
1.1 KiB
TypeScript
Raw Normal View History

import { Component, OnInit, Injectable } from '@angular/core';
import { webSocket, WebSocketSubject } from 'rxjs/webSocket';
import { catchError, tap, switchAll } from 'rxjs/operators';
import { EMPTY, Observer, Subject } from 'rxjs';
import { environment } from '../environments/environment';
@Injectable({
providedIn: 'root'
})
export class HermesSocketService implements OnInit {
private WS_ENDPOINT = environment.WSS_ENDPOINT;
private socket: WebSocketSubject<any> | undefined = undefined
constructor() { }
ngOnInit(): void {
}
public connect(): void {
if (!this.socket || this.socket.closed) {
this.socket = this.getNewWebSocket();
}
}
private getNewWebSocket() {
return webSocket({
2024-10-25 19:09:34 +00:00
url: environment.WSS_ENDPOINT
});
}
public sendMessage(msg: any) {
if (!this.socket || this.socket.closed)
return
this.socket.next(msg);
}
public subscribe(subscriptions: any) {
if (!this.socket || this.socket.closed)
return
return this.socket.subscribe(subscriptions);
}
public close() {
if (!this.socket || this.socket.closed)
return
this.socket.complete();
}
}