From 971a4b8c218ee89bec8270acb71530ed2abeea71 Mon Sep 17 00:00:00 2001 From: Sergey Yarkov Date: Thu, 10 Feb 2022 22:22:40 +0300 Subject: [PATCH] feat: added first impl of messenger --- app/Controllers/Ws/OnlineController.ts | 2 +- app/Services/WsService.ts | 24 ++++++++++++++++++++++-- app/Store/SessionStore.ts | 8 +++++++- 3 files changed, 30 insertions(+), 4 deletions(-) diff --git a/app/Controllers/Ws/OnlineController.ts b/app/Controllers/Ws/OnlineController.ts index 1d2c1fc..59b132b 100644 --- a/app/Controllers/Ws/OnlineController.ts +++ b/app/Controllers/Ws/OnlineController.ts @@ -23,7 +23,7 @@ export default class OnlineController { this.socket.on('disconnect', reason => this.onDisconnected(reason)); } - public sendOnline(online: string[]) { + public sendOnline(online: [string, { userId: string; userName: string }][]) { this.io.sockets.emit('user:online', online); } diff --git a/app/Services/WsService.ts b/app/Services/WsService.ts index 84c05dc..1e118e0 100644 --- a/app/Services/WsService.ts +++ b/app/Services/WsService.ts @@ -19,7 +19,12 @@ export interface ServerToClientEvents { */ 'user:session': (data: { sessionId: string | undefined; userId: string | undefined }) => void; 'user:connected': (data: { userId: string; userName: string }) => void; - 'user:online': (data: string[]) => void; + 'user:online': (data: [string, { userId: string; userName: string }][]) => void; + + /** + * Message + */ + 'message:private': (data: { content: string; from: string; to: string }) => void; } export interface ClientToServerEvents { @@ -28,6 +33,11 @@ export interface ClientToServerEvents { */ 'user:logout': () => void; 'user:status': () => void; + + /** + * Message + */ + 'message:private': (data: { content: string; to: string }) => void; } export interface InterServerEvents {} @@ -76,18 +86,28 @@ class WsService { } private listen() { - this.io.on('connection', socket => { + this.io.on('connection', async socket => { // eslint-disable-next-line @typescript-eslint/no-unused-vars const onlineController = new OnlineController(socket, this.io, this.sessionStore); const { sessionId, userId, userName } = socket.data; const isExistSocketData = !!(sessionId && userId && userName); + if (isExistSocketData) { + socket.join(userId); + } + /** * Send session to client */ socket.emit('user:session', { sessionId: socket.data.sessionId, userId: socket.data.userId }); + socket.on('message:private', ({ content, to }) => { + if (isExistSocketData) { + this.io.to(to).emit('message:private', { content, from: userId, to }); + } + }); + /** * Destroy session on user logout request */ diff --git a/app/Store/SessionStore.ts b/app/Store/SessionStore.ts index 9f6a935..6510075 100644 --- a/app/Store/SessionStore.ts +++ b/app/Store/SessionStore.ts @@ -72,7 +72,13 @@ class RedisSessionStore { */ public async getOnlineSessions() { const sessions = await this.getSessions(); - const online = new Set(sessions.filter((s): s is SessionDataType => s?.connected === true).map(s => s?.userId)); + const online = new Map(); + + sessions.forEach(s => { + if (s?.connected === true) { + online.set(s.userId, s); + } + }); return Array.from(online); }