Skip to content
Snippets Groups Projects
Commit e14f88a3 authored by paf's avatar paf
Browse files

use provided url for websocket (going to be authetification asd well)

parent f9f321a0
No related branches found
No related tags found
No related merge requests found
...@@ -62,26 +62,26 @@ export abstract class WebsocketBackendService<T> extends WebsocketService implem ...@@ -62,26 +62,26 @@ export abstract class WebsocketBackendService<T> extends WebsocketService implem
.get<T>(this.serverUrl + this.pollingEndpoint, {observe: 'response'}) .get<T>(this.serverUrl + this.pollingEndpoint, {observe: 'response'})
.pipe( .pipe(
catchError((err: ApiError) => { catchError((err: ApiError) => {
console.warn(`Api-Error: ${err.code} ${err.info}`); console.warn(`Api-Error: ${err.code} ${err.info}`);
this.connectionStatus$.next("error"); this.connectionStatus$.next("error");
return of([]) return of([])
}) })
) )
.subscribe((response: HttpResponse<T>) => { .subscribe((response: HttpResponse<T>) => {
this.data$.next(response.body); this.data$.next(response.body);
if (response.headers.has('SubscribeURI')) { if (response.headers.has('SubscribeURI')) {
console.log('switch to websocket-mode'); this.wsUrl = response.headers.get('SubscribeURI');
this.urlParam = response.headers.get('SubscribeURI'); console.log('switch to websocket-mode');
this.subScribeToStatusUpdateWsChannel(); this.subScribeToWsChannel();
} else { } else {
this.connectionStatus$.next("polling-sleep"); this.connectionStatus$.next("polling-sleep");
this.scheduleNextPoll(); this.scheduleNextPoll();
} }
}); });
} }
...@@ -122,12 +122,12 @@ export abstract class WebsocketBackendService<T> extends WebsocketService implem ...@@ -122,12 +122,12 @@ export abstract class WebsocketBackendService<T> extends WebsocketService implem
} }
private subScribeToStatusUpdateWsChannel() { private subScribeToWsChannel() {
this.wsDataSubscription = this.getChannel<T>(this.wsChannelName) this.wsDataSubscription = this.getChannel<T>(this.wsChannelName)
.subscribe((dataObject: T) => this.data$.next(dataObject)); // subscribe only next, not complete! .subscribe((dataObject: T) => this.data$.next(dataObject)); // subscribe only next, not complete!
this.wsConnectionStatusSubscription = this.serviceConnected$ this.wsConnectionStatusSubscription = this.wsConnected$
.pipe( .pipe(
skipWhile((item: boolean) => item === null), // skip pre-init-state skipWhile((item: boolean) => item === null), // skip pre-init-state
tap((wsConnected: boolean) => { tap((wsConnected: boolean) => {
......
...@@ -10,12 +10,11 @@ interface WsMessage { ...@@ -10,12 +10,11 @@ interface WsMessage {
export class WebsocketService { export class WebsocketService {
protected url = 'ws://127.0.0.1:3000'; protected wsUrl: string = "";
protected urlParam = "XYZ";
private webSocketSubject$: WebSocketSubject<any>; private wsSubject$: WebSocketSubject<any>;
public serviceConnected$ = new BehaviorSubject<boolean>(null); public wsConnected$ = new BehaviorSubject<boolean>(null);
constructor( constructor(
...@@ -23,17 +22,13 @@ export class WebsocketService { ...@@ -23,17 +22,13 @@ export class WebsocketService {
} }
public connect(urlParam: string, forceReconnect: boolean = false): WebSocketSubject<any> { public connect() {
this.urlParam = urlParam; if (!this.wsSubject$) {
// const url = 'wss://echo.websocket.org'; console.log('connecting...' + this.wsUrl);
if (!this.webSocketSubject$ || forceReconnect) { this.wsSubject$ = webSocket({
console.log('connecting...' + urlParam);
this.webSocketSubject$ = webSocket({
deserializer(event: MessageEvent): any { deserializer(event: MessageEvent): any {
return JSON.parse(event.data); return JSON.parse(event.data);
...@@ -46,50 +41,48 @@ export class WebsocketService { ...@@ -46,50 +41,48 @@ export class WebsocketService {
openObserver: { openObserver: {
next: () => { next: () => {
console.log('connection established'); console.log('connection established');
this.serviceConnected$.next(true); this.wsConnected$.next(true);
} }
}, },
url: this.url + '/' + urlParam url: this.wsUrl
}); });
this.webSocketSubject$.subscribe( this.wsSubject$.subscribe(
() => {}, () => {},
() => { () => {
console.log('connection error'); console.log('connection error');
this.serviceConnected$.next(false); this.wsConnected$.next(false);
}, },
() => { () => {
console.log('connection closed'); console.log('connection closed');
this.serviceConnected$.next(false); this.wsConnected$.next(false);
} }
); );
} }
return this.webSocketSubject$;
} }
public send(event: string, data: any) { public send(event: string, data: any) {
if (!this.webSocketSubject$) { if (!this.wsSubject$) {
this.connect(this.urlParam); this.connect();
} }
this.webSocketSubject$.next({event, data}); this.wsSubject$.next({event, data});
} }
public getChannel<T>(channelName: string): Observable<T> { public getChannel<T>(channelName: string): Observable<T> {
if (!this.webSocketSubject$) { if (!this.wsSubject$) {
this.connect(this.urlParam); this.connect();
} }
return this.webSocketSubject$ return this.wsSubject$
.multiplex( .multiplex(
() => ({event: `subscribe:${channelName}`}), () => ({event: `subscribe:${channelName}`}),
() => ({event: `unsubscribe:${channelName}`}), () => ({event: `unsubscribe:${channelName}`}),
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment