Handle authentication in the same cmds packet.

This commit is contained in:
Igor Kulikov 2023-12-11 13:50:24 +02:00
parent 1d674046d3
commit 5d7ba1f88b
4 changed files with 12 additions and 8 deletions

View File

@ -188,6 +188,7 @@ public class TbWebSocketHandler extends TextWebSocketHandler implements WebSocke
sessionRef.setSecurityCtx(securityCtx); sessionRef.setSecurityCtx(securityCtx);
pendingSessions.invalidate(sessionMd.session.getId()); pendingSessions.invalidate(sessionMd.session.getId());
establishSession(sessionMd.session, sessionRef, sessionMd); establishSession(sessionMd.session, sessionRef, sessionMd);
webSocketService.handleCommands(sessionRef, cmdsWrapper);
} }
} }

View File

@ -22,7 +22,6 @@ 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 { import {
AuthCmd,
AuthWsCmd, AuthWsCmd,
CmdUpdateMsg, CmdUpdateMsg,
NotificationSubscriber, NotificationSubscriber,
@ -196,7 +195,7 @@ export abstract class WebsocketService<T extends WsSubscriber> implements WsServ
private onOpen(token: string) { private onOpen(token: string) {
this.isOpening = false; this.isOpening = false;
this.isOpened = true; this.isOpened = true;
this.dataStream.next(this.createdAuthMsg(token)); this.cmdWrapper.setAuth(token);
if (this.reconnectTimer) { if (this.reconnectTimer) {
clearTimeout(this.reconnectTimer); clearTimeout(this.reconnectTimer);
this.reconnectTimer = null; this.reconnectTimer = null;
@ -266,10 +265,4 @@ export abstract class WebsocketService<T extends WsSubscriber> implements WsServ
message, type: 'error' message, type: 'error'
})); }));
} }
private createdAuthMsg(token: string): AuthWsCmd {
return {
authCmd: new AuthCmd(token)
};
}
} }

View File

@ -348,6 +348,7 @@ export class TelemetryPluginCmdsWrapper implements CmdWrapper {
} }
cmds: Array<WebsocketCmd>; cmds: Array<WebsocketCmd>;
authCmd: AuthCmd;
private static popCmds<T>(cmds: Array<T>, leftCount: number): Array<T> { private static popCmds<T>(cmds: Array<T>, leftCount: number): Array<T> {
const toPublish = Math.min(cmds.length, leftCount); const toPublish = Math.min(cmds.length, leftCount);
@ -358,6 +359,10 @@ export class TelemetryPluginCmdsWrapper implements CmdWrapper {
} }
} }
public setAuth(token: string) {
this.authCmd = new AuthCmd(token);
}
public hasCommands(): boolean { public hasCommands(): boolean {
return this.cmds.length > 0; return this.cmds.length > 0;
} }
@ -368,6 +373,10 @@ export class TelemetryPluginCmdsWrapper implements CmdWrapper {
public preparePublishCommands(maxCommands: number): TelemetryPluginCmdsWrapper { public preparePublishCommands(maxCommands: number): TelemetryPluginCmdsWrapper {
const preparedWrapper = new TelemetryPluginCmdsWrapper(); const preparedWrapper = new TelemetryPluginCmdsWrapper();
if (this.authCmd) {
preparedWrapper.authCmd = this.authCmd;
this.authCmd = null;
}
preparedWrapper.cmds = TelemetryPluginCmdsWrapper.popCmds(this.cmds, maxCommands); preparedWrapper.cmds = TelemetryPluginCmdsWrapper.popCmds(this.cmds, maxCommands);
return preparedWrapper; return preparedWrapper;
} }

View File

@ -25,6 +25,7 @@ export interface WsService<T extends WsSubscriber> {
} }
export abstract class CmdWrapper { export abstract class CmdWrapper {
abstract setAuth(token: string);
abstract hasCommands(): boolean; abstract hasCommands(): boolean;
abstract clear(): void; abstract clear(): void;
abstract preparePublishCommands(maxCommands: number): CmdWrapper; abstract preparePublishCommands(maxCommands: number): CmdWrapper;