Skip to content

Commit

Permalink
Update socket io
Browse files Browse the repository at this point in the history
  • Loading branch information
havfo committed Feb 18, 2024
1 parent 47aa0eb commit 5b8ba6e
Show file tree
Hide file tree
Showing 4 changed files with 149 additions and 3 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
"edumeet-common": "edumeet/edumeet-common#0.5.0",
"mediasoup": "^3.13.19",
"minimist": "^1.2.8",
"socket.io": "^4.6.1"
"socket.io": "^4.7.4"
},
"optionalDependencies": {
"bufferutil": "^4.0.7",
Expand Down
145 changes: 145 additions & 0 deletions src/common/IOServerConnection.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
import {
BaseConnection,
Logger,
SocketMessage,
SocketTimeoutError,
skipIfClosed,
} from 'edumeet-common';
import { Socket } from 'socket.io';

interface ClientServerEvents {
/* eslint-disable no-unused-vars */
notification: (notification: SocketMessage) => void;
request: (request: SocketMessage, result: (
serverError: unknown | null,
responseData: unknown) => void
) => void;
/* eslint-enable no-unused-vars */
}

interface ServerClientEvents {
/* eslint-disable no-unused-vars */
notification: (notification: SocketMessage) => void;
request: (request: SocketMessage, result: (
timeout: Error | null,
serverError: unknown | null,
responseData: unknown) => void
) => void;
/* eslint-enable no-unused-vars */
}

export type clientAddress = {
address: string
forwardedFor?: string | string[]
}

const logger = new Logger('SocketIOConnection');

export class IOServerConnection extends BaseConnection {
public closed = false;
private socket: Socket<ClientServerEvents, ServerClientEvents>;

constructor(socket: Socket<ClientServerEvents, ServerClientEvents>) {
super();

logger.debug('constructor()');

this.socket = socket;
this.handleSocket();
}

@skipIfClosed
public close(): void {
logger.debug('close() [id: %s]', this.id);

this.closed = true;

if (this.socket.connected)
this.socket.disconnect(true);

this.socket.removeAllListeners();

this.emit('close');
}

public get id(): string {
return this.socket.id;
}

public get address(): clientAddress {
const address: clientAddress = {
address: this.socket.handshake.address,
forwardedFor: this.socket.handshake.headers['x-forwarded-for']
};

return address;
}

@skipIfClosed
public notify(notification: SocketMessage): void {
logger.debug('notification() [notification: %o]', notification);

this.socket.emit('notification', notification);
}

@skipIfClosed
private sendRequestOnWire(socketMessage: SocketMessage): Promise<unknown> {
return new Promise((resolve, reject) => {
if (!this.socket) {
reject('No socket connection');
} else {
this.socket.timeout(3000).emit('request', socketMessage, (timeout, serverError, response) => {
if (timeout) reject(new SocketTimeoutError('Request timed out'));
else if (serverError) reject(serverError);
else resolve(response);
});
}
});
}

@skipIfClosed
public async request(request: SocketMessage): Promise<unknown> {
logger.debug('sendRequest() [request: %o]', request);

for (let tries = 0; tries < 3; tries++) {
try {
return await this.sendRequestOnWire(request);
} catch (error) {
if (error instanceof SocketTimeoutError)
logger.warn('sendRequest() timeout, retrying [attempt: %s]', tries);
else
throw error;
}
}
}

private handleSocket(): void {
logger.debug('handleSocket()');

// TODO: reconnect logic here
this.socket.once('disconnect', () => {
logger.debug('socket disconnected');

this.close();
});

this.socket.on('notification', (notification) => {
logger.debug('"notification" event [notification: %o]', notification);

this.emit('notification', notification);
});

this.socket.on('request', (request, result) => {
logger.debug('"request" event [request: %o]', request);

this.emit(
'request',
request,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(response: any) => result(null, response),
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(error: any) => result(error, null)
);
});
}
}
3 changes: 2 additions & 1 deletion src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ import { interactiveServer, interactiveServerAddMediaService } from './interacti
import MediaService from './MediaService';
import RoomServer from './RoomServer';
import { RoomServerConnection } from './RoomServerConnection';
import { IOServerConnection, Logger } from 'edumeet-common';
import { Logger } from 'edumeet-common';
import { createHttpEndpoints } from './httpEndpoints';
import { IOServerConnection } from './common/IOServerConnection';

const logger = new Logger('MediaNode');

Expand Down
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3215,7 +3215,7 @@ socket.io@^2.0.3:
socket.io-client "2.5.0"
socket.io-parser "~3.4.0"

socket.io@^4.6.1:
socket.io@^4.7.4:
version "4.7.4"
resolved "https://registry.yarnpkg.com/socket.io/-/socket.io-4.7.4.tgz#2401a2d7101e4bdc64da80b140d5d8b6a8c7738b"
integrity sha512-DcotgfP1Zg9iP/dH9zvAQcWrE0TtbMVwXmlV4T4mqsvY+gw+LqUGPfx2AoVyRk0FLME+GQhufDMyacFmw7ksqw==
Expand Down

0 comments on commit 5b8ba6e

Please sign in to comment.