53 lines
1.1 KiB
TypeScript
53 lines
1.1 KiB
TypeScript
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({
|
|
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();
|
|
}
|
|
}
|