UI: Added auth msg in WS

This commit is contained in:
Vladyslav_Prykhodko 2023-12-08 18:05:47 +02:00
parent c21f0d025c
commit 07dd8f65e5
2 changed files with 34 additions and 6 deletions

View File

@ -21,10 +21,16 @@ import { AuthService } from '@core/auth/auth.service';
import { NgZone } from '@angular/core'; import { NgZone } from '@angular/core';
import { selectIsAuthenticated } from '@core/auth/auth.selectors'; import { selectIsAuthenticated } from '@core/auth/auth.selectors';
import { webSocket, WebSocketSubject } from 'rxjs/webSocket'; import { webSocket, WebSocketSubject } from 'rxjs/webSocket';
import { CmdUpdateMsg, TelemetrySubscriber, WebsocketDataMsg } from '@shared/models/telemetry/telemetry.models'; import {
AuthCmd,
AuthWsCmd,
CmdUpdateMsg,
TelemetrySubscriber,
WebsocketDataMsg
} from '@shared/models/telemetry/telemetry.models';
import { ActionNotificationShow } from '@core/notification/notification.actions'; import { ActionNotificationShow } from '@core/notification/notification.actions';
import Timeout = NodeJS.Timeout;
import { NotificationSubscriber } from '@shared/models/websocket/notification-ws.models'; import { NotificationSubscriber } from '@shared/models/websocket/notification-ws.models';
import Timeout = NodeJS.Timeout;
const RECONNECT_INTERVAL = 2000; const RECONNECT_INTERVAL = 2000;
const WS_IDLE_TIMEOUT = 90000; const WS_IDLE_TIMEOUT = 90000;
@ -48,7 +54,7 @@ export abstract class WebsocketService<T extends WsSubscriber> implements WsServ
wsUri: string; wsUri: string;
dataStream: WebSocketSubject<CmdWrapper | CmdUpdateMsg>; dataStream: WebSocketSubject<CmdWrapper | CmdUpdateMsg | AuthWsCmd>;
errorName = 'WebSocket Error'; errorName = 'WebSocket Error';
@ -158,13 +164,13 @@ export abstract class WebsocketService<T extends WsSubscriber> implements WsServ
} }
private openSocket(token: string) { private openSocket(token: string) {
const uri = `${this.wsUri}?token=${token}`; const uri = `${this.wsUri}`;
this.dataStream = webSocket<CmdUpdateMsg>( this.dataStream = webSocket<CmdUpdateMsg>(
{ {
url: uri, url: uri,
openObserver: { openObserver: {
next: () => { next: () => {
this.onOpen(); this.onOpen(token);
} }
}, },
closeObserver: { closeObserver: {
@ -187,9 +193,10 @@ export abstract class WebsocketService<T extends WsSubscriber> implements WsServ
}); });
} }
private onOpen() { private onOpen(token: string) {
this.isOpening = false; this.isOpening = false;
this.isOpened = true; this.isOpened = true;
this.dataStream.next(this.createdAuthMsg(token));
if (this.reconnectTimer) { if (this.reconnectTimer) {
clearTimeout(this.reconnectTimer); clearTimeout(this.reconnectTimer);
this.reconnectTimer = null; this.reconnectTimer = null;
@ -260,4 +267,9 @@ export abstract class WebsocketService<T extends WsSubscriber> implements WsServ
})); }));
} }
private createdAuthMsg(token: string): AuthWsCmd {
return {
authCmd: new AuthCmd(token)
};
}
} }

View File

@ -122,6 +122,8 @@ export enum DataSortOrder {
} }
export enum WsCmdType { export enum WsCmdType {
AUTH = 'AUTH',
ATTRIBUTES = 'ATTRIBUTES', ATTRIBUTES = 'ATTRIBUTES',
TIMESERIES = 'TIMESERIES', TIMESERIES = 'TIMESERIES',
TIMESERIES_HISTORY = 'TIMESERIES_HISTORY', TIMESERIES_HISTORY = 'TIMESERIES_HISTORY',
@ -147,6 +149,10 @@ export interface WebsocketCmd {
type: WsCmdType; type: WsCmdType;
} }
export interface AuthWsCmd {
authCmd: AuthCmd;
}
export interface TelemetryPluginCmd extends WebsocketCmd { export interface TelemetryPluginCmd extends WebsocketCmd {
keys: string; keys: string;
} }
@ -289,6 +295,16 @@ export class AlarmCountUnsubscribeCmd implements WebsocketCmd {
type = WsCmdType.ALARM_COUNT_UNSUBSCRIBE; type = WsCmdType.ALARM_COUNT_UNSUBSCRIBE;
} }
export class AuthCmd implements WebsocketCmd {
cmdId = 0;
type: WsCmdType.AUTH;
token: string;
constructor(token: string) {
this.token = token;
}
}
export class TelemetryPluginCmdsWrapper implements CmdWrapper { export class TelemetryPluginCmdsWrapper implements CmdWrapper {
constructor() { constructor() {