-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
149 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters