Skip to content
This repository has been archived by the owner on Oct 18, 2024. It is now read-only.

Commit

Permalink
Merge pull request #726 from SuperViz/feat/same-account-error-ioc
Browse files Browse the repository at this point in the history
feat: add same account validation
  • Loading branch information
carlossantos74 authored Jul 23, 2024
2 parents d004243 + e5082a5 commit 515bcbb
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 36 deletions.
3 changes: 3 additions & 0 deletions src/core/launcher/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down
30 changes: 22 additions & 8 deletions src/core/launcher/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<string, Participant> = {};

Expand All @@ -327,9 +323,24 @@ export class Launcher extends Observable implements DefaultLauncher {
);
this.room.presence.on<Participant>(Socket.PresenceEvents.LEAVE, this.onParticipantLeaveIOC);
this.room.presence.on<Participant>(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;
}
};

/**
Expand All @@ -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);
Expand Down
11 changes: 8 additions & 3 deletions src/lib/socket/connection/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -116,13 +115,19 @@ export class ClientConnection {
private onCustomError = (error: SocketErrorEvent) => {
if (error.needsToDisconnect) {
this.socket.disconnect();
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);
};
}
6 changes: 5 additions & 1 deletion src/lib/socket/connection/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
30 changes: 6 additions & 24 deletions src/services/io/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.',
Expand All @@ -62,22 +50,16 @@ export class IOC {
return;
}

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
Expand Down
1 change: 1 addition & 0 deletions src/services/io/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ export enum IOCState {
RECONNECTING = 'RECONNECTING',
RECONNECT_ERROR = 'RECONNECT_ERROR',
AUTH_ERROR = 'AUTH_ERROR',
SAME_ACCOUNT_ERROR = 'SAME_ACCOUNT_ERROR',
}

0 comments on commit 515bcbb

Please sign in to comment.