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

Commit 515bcbb

Browse files
Merge pull request #726 from SuperViz/feat/same-account-error-ioc
feat: add same account validation
2 parents d004243 + e5082a5 commit 515bcbb

File tree

6 files changed

+45
-36
lines changed

6 files changed

+45
-36
lines changed

src/core/launcher/index.test.ts

+3
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ describe('Launcher', () => {
5050
localParticipant.value = MOCK_LOCAL_PARTICIPANT;
5151

5252
LauncherInstance = new Launcher(DEFAULT_INITIALIZATION_MOCK);
53+
54+
const { hasJoinedRoom } = useStore(StoreType.GLOBAL);
55+
hasJoinedRoom.publish(true);
5356
});
5457

5558
test('should be defined', () => {

src/core/launcher/index.ts

+22-8
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,7 @@ export class Launcher extends Observable implements DefaultLauncher {
184184
this.room?.presence.off(Socket.PresenceEvents.JOINED_ROOM);
185185
this.room?.presence.off(Socket.PresenceEvents.LEAVE);
186186
this.room?.presence.off(Socket.PresenceEvents.UPDATE);
187+
this.ioc.stateSubject.unsubscribe();
187188
this.ioc?.destroy();
188189

189190
this.isDestroyed = true;
@@ -294,13 +295,8 @@ export class Launcher extends Observable implements DefaultLauncher {
294295
private startIOC = (): void => {
295296
this.logger.log('launcher service @ startIOC');
296297
const { participants } = useStore(StoreType.GLOBAL);
297-
// retrieve the current participants in the room
298298

299-
this.ioc.stateSubject.subscribe((state) => {
300-
if (state === IOCState.AUTH_ERROR) {
301-
this.onAuthentication(false);
302-
}
303-
});
299+
this.ioc.stateSubject.subscribe(this.onConnectionStateChange);
304300
this.room.presence.get((presences) => {
305301
const participantsMap: Record<string, Participant> = {};
306302

@@ -327,9 +323,24 @@ export class Launcher extends Observable implements DefaultLauncher {
327323
);
328324
this.room.presence.on<Participant>(Socket.PresenceEvents.LEAVE, this.onParticipantLeaveIOC);
329325
this.room.presence.on<Participant>(Socket.PresenceEvents.UPDATE, this.onParticipantUpdatedIOC);
326+
};
330327

331-
const { hasJoinedRoom } = useStore(StoreType.GLOBAL);
332-
hasJoinedRoom.publish(true);
328+
/**
329+
* @function onConnectionStateChange
330+
* @description on connection state change
331+
* @param state - connection state
332+
* @returns {void}
333+
*/
334+
private onConnectionStateChange = (state: IOCState): void => {
335+
if (state === IOCState.AUTH_ERROR) {
336+
this.onAuthentication(false);
337+
return;
338+
}
339+
340+
if (state === IOCState.SAME_ACCOUNT_ERROR) {
341+
this.onSameAccount();
342+
return;
343+
}
333344
};
334345

335346
/**
@@ -347,6 +358,9 @@ export class Launcher extends Observable implements DefaultLauncher {
347358

348359
this.logger.log('launcher service @ onParticipantJoined - local participant joined');
349360

361+
const { hasJoinedRoom } = useStore(StoreType.GLOBAL);
362+
hasJoinedRoom.publish(true);
363+
350364
this.attachComponentsAfterJoin();
351365
this.publish(ParticipantEvent.LOCAL_JOINED, this.participant);
352366
this.publish(ParticipantEvent.JOINED, this.participant);

src/lib/socket/connection/index.ts

+8-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import type { Socket } from 'socket.io-client';
33

44
import { ErrorCallback } from '../common/types/callbacks.types';
55

6-
76
import { ClientState, ConnectionState, SocketErrorEvent, SocketEvent } from './types';
87
import { Logger } from '../../../common/utils';
98

@@ -116,13 +115,19 @@ export class ClientConnection {
116115
private onCustomError = (error: SocketErrorEvent) => {
117116
if (error.needsToDisconnect) {
118117
this.socket.disconnect();
118+
this.changeState(ClientState.DISCONNECTED, error.errorType);
119119
}
120120

121+
const logMessage = `[SuperViz]
122+
- Error: ${error.errorType}
123+
- Message: ${error.message}
124+
`;
125+
121126
if (error.level === 'error') {
122-
console.error('[SuperViz - Error]', 'Type: ', error.errorType, 'Message :', error.message);
127+
console.error(logMessage);
123128
return;
124129
}
125130

126-
console.warn('[SuperViz - Warning]', 'Type: ', error.errorType, 'Message :', error.message);
131+
console.warn(logMessage);
127132
};
128133
}

src/lib/socket/connection/types.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,11 @@ export interface ConnectionState {
2929
}
3030

3131
export type SocketErrorEvent = {
32-
errorType: 'message-size-limit' | 'rate-limit' | 'room-connections-limit';
32+
errorType:
33+
| 'message-size-limit'
34+
| 'rate-limit'
35+
| 'room-connections-limit'
36+
| 'user-already-in-room';
3337
message: string;
3438
connectionId: string;
3539
needsToDisconnect: boolean;

src/services/io/index.ts

+6-24
Original file line numberDiff line numberDiff line change
@@ -35,18 +35,6 @@ export class IOC {
3535
}
3636

3737
private handleConnectionState = (state: Socket.ConnectionState): void => {
38-
// const needsToReconnectStates = [
39-
// Socket.ClientState.DISCONNECTED,
40-
// Socket.ClientState.RECONNECT_ERROR,
41-
// ];
42-
43-
// if (
44-
// needsToReconnectStates.includes(state.state) &&
45-
// !['io client disconnect', 'Unauthorized connection'].includes(state.reason)
46-
// ) {
47-
// this.forceReconnect();
48-
// }
49-
5038
if (state.reason === 'Unauthorized connection') {
5139
console.error(
5240
'[Superviz] Unauthorized connection. Please check your API key and if your domain is white listed.',
@@ -62,22 +50,16 @@ export class IOC {
6250
return;
6351
}
6452

53+
if (state.reason === 'user-already-in-room') {
54+
this.state = state;
55+
this.stateSubject.next(IOCState.SAME_ACCOUNT_ERROR);
56+
return;
57+
}
58+
6559
this.state = state;
6660
this.stateSubject.next(state.state as unknown as IOCState);
6761
};
6862

69-
/**
70-
* @function forceReconnect
71-
* @description force the socket to reconnect
72-
* @returns {void}
73-
*/
74-
private forceReconnect(): void {
75-
this.client?.destroy();
76-
this.client = null;
77-
78-
this.createClient();
79-
}
80-
8163
/**
8264
* @function createClient
8365
* @description create a new socket client

src/services/io/types.ts

+1
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ export enum IOCState {
66
RECONNECTING = 'RECONNECTING',
77
RECONNECT_ERROR = 'RECONNECT_ERROR',
88
AUTH_ERROR = 'AUTH_ERROR',
9+
SAME_ACCOUNT_ERROR = 'SAME_ACCOUNT_ERROR',
910
}

0 commit comments

Comments
 (0)