From 29ad1f6adf940a96fb676ca0f970b36ecb730fa3 Mon Sep 17 00:00:00 2001 From: Carlos Date: Mon, 22 Jul 2024 18:46:46 -0300 Subject: [PATCH 1/4] feat: add same account validation --- src/core/launcher/index.ts | 30 ++++++++++++++++++++-------- src/lib/socket/connection/index.ts | 3 ++- src/lib/socket/connection/types.ts | 6 +++++- src/services/io/index.ts | 32 ++++++++---------------------- src/services/io/types.ts | 1 + 5 files changed, 38 insertions(+), 34 deletions(-) diff --git a/src/core/launcher/index.ts b/src/core/launcher/index.ts index 50c1ac8c..af6d6ffb 100644 --- a/src/core/launcher/index.ts +++ b/src/core/launcher/index.ts @@ -184,6 +184,7 @@ export class Launcher extends Observable implements DefaultLauncher { this.room?.presence.off(Socket.PresenceEvents.JOINED_ROOM); this.room?.presence.off(Socket.PresenceEvents.LEAVE); this.room?.presence.off(Socket.PresenceEvents.UPDATE); + this.ioc.stateSubject.unsubscribe(); this.ioc?.destroy(); this.isDestroyed = true; @@ -294,13 +295,8 @@ export class Launcher extends Observable implements DefaultLauncher { private startIOC = (): void => { this.logger.log('launcher service @ startIOC'); const { participants } = useStore(StoreType.GLOBAL); - // retrieve the current participants in the room - this.ioc.stateSubject.subscribe((state) => { - if (state === IOCState.AUTH_ERROR) { - this.onAuthentication(false); - } - }); + this.ioc.stateSubject.subscribe(this.onConnectionStateChange); this.room.presence.get((presences) => { const participantsMap: Record = {}; @@ -327,9 +323,24 @@ export class Launcher extends Observable implements DefaultLauncher { ); this.room.presence.on(Socket.PresenceEvents.LEAVE, this.onParticipantLeaveIOC); this.room.presence.on(Socket.PresenceEvents.UPDATE, this.onParticipantUpdatedIOC); + }; - const { hasJoinedRoom } = useStore(StoreType.GLOBAL); - hasJoinedRoom.publish(true); + /** + * @function onConnectionStateChange + * @description on connection state change + * @param state - connection state + * @returns {void} + */ + private onConnectionStateChange = (state: IOCState): void => { + if (state === IOCState.AUTH_ERROR) { + this.onAuthentication(false); + return; + } + + if (state === IOCState.SAME_ACCOUNT_ERROR) { + this.onSameAccount(); + return; + } }; /** @@ -347,6 +358,9 @@ export class Launcher extends Observable implements DefaultLauncher { this.logger.log('launcher service @ onParticipantJoined - local participant joined'); + const { hasJoinedRoom } = useStore(StoreType.GLOBAL); + hasJoinedRoom.publish(true); + this.attachComponentsAfterJoin(); this.publish(ParticipantEvent.LOCAL_JOINED, this.participant); this.publish(ParticipantEvent.JOINED, this.participant); diff --git a/src/lib/socket/connection/index.ts b/src/lib/socket/connection/index.ts index 6a1f8b3f..cab6906d 100644 --- a/src/lib/socket/connection/index.ts +++ b/src/lib/socket/connection/index.ts @@ -3,7 +3,6 @@ import type { Socket } from 'socket.io-client'; import { ErrorCallback } from '../common/types/callbacks.types'; - import { ClientState, ConnectionState, SocketErrorEvent, SocketEvent } from './types'; import { Logger } from '../../../common/utils'; @@ -118,6 +117,8 @@ export class ClientConnection { this.socket.disconnect(); } + this.changeState(ClientState.DISCONNECTED, error.errorType); + if (error.level === 'error') { console.error('[SuperViz - Error]', 'Type: ', error.errorType, 'Message :', error.message); return; diff --git a/src/lib/socket/connection/types.ts b/src/lib/socket/connection/types.ts index dde82aab..1fdde25e 100644 --- a/src/lib/socket/connection/types.ts +++ b/src/lib/socket/connection/types.ts @@ -29,7 +29,11 @@ export interface ConnectionState { } export type SocketErrorEvent = { - errorType: 'message-size-limit' | 'rate-limit' | 'room-connections-limit'; + errorType: + | 'message-size-limit' + | 'rate-limit' + | 'room-connections-limit' + | 'user-already-in-room'; message: string; connectionId: string; needsToDisconnect: boolean; diff --git a/src/services/io/index.ts b/src/services/io/index.ts index a92cd255..1c3e7b27 100644 --- a/src/services/io/index.ts +++ b/src/services/io/index.ts @@ -35,18 +35,6 @@ export class IOC { } private handleConnectionState = (state: Socket.ConnectionState): void => { - // const needsToReconnectStates = [ - // Socket.ClientState.DISCONNECTED, - // Socket.ClientState.RECONNECT_ERROR, - // ]; - - // if ( - // needsToReconnectStates.includes(state.state) && - // !['io client disconnect', 'Unauthorized connection'].includes(state.reason) - // ) { - // this.forceReconnect(); - // } - if (state.reason === 'Unauthorized connection') { console.error( '[Superviz] Unauthorized connection. Please check your API key and if your domain is white listed.', @@ -62,22 +50,18 @@ export class IOC { return; } + console.log('[Superviz] Connection state:', state); + + if (state.reason === 'user-already-in-room') { + this.state = state; + this.stateSubject.next(IOCState.SAME_ACCOUNT_ERROR); + return; + } + this.state = state; this.stateSubject.next(state.state as unknown as IOCState); }; - /** - * @function forceReconnect - * @description force the socket to reconnect - * @returns {void} - */ - private forceReconnect(): void { - this.client?.destroy(); - this.client = null; - - this.createClient(); - } - /** * @function createClient * @description create a new socket client diff --git a/src/services/io/types.ts b/src/services/io/types.ts index 61514ccf..73d0f192 100644 --- a/src/services/io/types.ts +++ b/src/services/io/types.ts @@ -6,4 +6,5 @@ export enum IOCState { RECONNECTING = 'RECONNECTING', RECONNECT_ERROR = 'RECONNECT_ERROR', AUTH_ERROR = 'AUTH_ERROR', + SAME_ACCOUNT_ERROR = 'SAME_ACCOUNT_ERROR', } From fa3dd37fa503aca6f79c99565fcbe7843aa0c1af Mon Sep 17 00:00:00 2001 From: Carlos Date: Mon, 22 Jul 2024 18:49:35 -0300 Subject: [PATCH 2/4] test: mock joined room --- src/core/launcher/index.test.ts | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/core/launcher/index.test.ts b/src/core/launcher/index.test.ts index 0eed5900..2e262eee 100644 --- a/src/core/launcher/index.test.ts +++ b/src/core/launcher/index.test.ts @@ -50,6 +50,9 @@ describe('Launcher', () => { localParticipant.value = MOCK_LOCAL_PARTICIPANT; LauncherInstance = new Launcher(DEFAULT_INITIALIZATION_MOCK); + + const { hasJoinedRoom } = useStore(StoreType.GLOBAL); + hasJoinedRoom.publish(true); }); test('should be defined', () => { From 02a0b3e79daf367395f11a119b14f81e2a840dec Mon Sep 17 00:00:00 2001 From: Carlos Date: Mon, 22 Jul 2024 19:48:15 -0300 Subject: [PATCH 3/4] fix: only change state to disconnected when needs to disconnect --- src/lib/socket/connection/index.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/lib/socket/connection/index.ts b/src/lib/socket/connection/index.ts index cab6906d..4b33db66 100644 --- a/src/lib/socket/connection/index.ts +++ b/src/lib/socket/connection/index.ts @@ -115,10 +115,9 @@ export class ClientConnection { private onCustomError = (error: SocketErrorEvent) => { if (error.needsToDisconnect) { this.socket.disconnect(); + this.changeState(ClientState.DISCONNECTED, error.errorType); } - this.changeState(ClientState.DISCONNECTED, error.errorType); - if (error.level === 'error') { console.error('[SuperViz - Error]', 'Type: ', error.errorType, 'Message :', error.message); return; From e5082a523903d9d02be0bd1b3a975b048fb44fc2 Mon Sep 17 00:00:00 2001 From: Carlos Date: Mon, 22 Jul 2024 19:58:10 -0300 Subject: [PATCH 4/4] refactor: improve console messages --- src/lib/socket/connection/index.ts | 9 +++++++-- src/services/io/index.ts | 2 -- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/src/lib/socket/connection/index.ts b/src/lib/socket/connection/index.ts index 4b33db66..9affaa34 100644 --- a/src/lib/socket/connection/index.ts +++ b/src/lib/socket/connection/index.ts @@ -118,11 +118,16 @@ export class ClientConnection { this.changeState(ClientState.DISCONNECTED, error.errorType); } + const logMessage = `[SuperViz] + - Error: ${error.errorType} + - Message: ${error.message} + `; + if (error.level === 'error') { - console.error('[SuperViz - Error]', 'Type: ', error.errorType, 'Message :', error.message); + console.error(logMessage); return; } - console.warn('[SuperViz - Warning]', 'Type: ', error.errorType, 'Message :', error.message); + console.warn(logMessage); }; } diff --git a/src/services/io/index.ts b/src/services/io/index.ts index 1c3e7b27..fce6b783 100644 --- a/src/services/io/index.ts +++ b/src/services/io/index.ts @@ -50,8 +50,6 @@ export class IOC { return; } - console.log('[Superviz] Connection state:', state); - if (state.reason === 'user-already-in-room') { this.state = state; this.stateSubject.next(IOCState.SAME_ACCOUNT_ERROR);