feat: added first impl of messenger

This commit is contained in:
Sergey Yarkov 2022-02-10 22:22:40 +03:00
parent 7b75877bd7
commit 971a4b8c21
3 changed files with 30 additions and 4 deletions

View File

@ -23,7 +23,7 @@ export default class OnlineController {
this.socket.on('disconnect', reason => this.onDisconnected(reason)); 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); this.io.sockets.emit('user:online', online);
} }

View File

@ -19,7 +19,12 @@ export interface ServerToClientEvents {
*/ */
'user:session': (data: { sessionId: string | undefined; userId: string | undefined }) => void; 'user:session': (data: { sessionId: string | undefined; userId: string | undefined }) => void;
'user:connected': (data: { userId: string; userName: string }) => 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 { export interface ClientToServerEvents {
@ -28,6 +33,11 @@ export interface ClientToServerEvents {
*/ */
'user:logout': () => void; 'user:logout': () => void;
'user:status': () => void; 'user:status': () => void;
/**
* Message
*/
'message:private': (data: { content: string; to: string }) => void;
} }
export interface InterServerEvents {} export interface InterServerEvents {}
@ -76,18 +86,28 @@ class WsService {
} }
private listen() { private listen() {
this.io.on('connection', socket => { this.io.on('connection', async socket => {
// eslint-disable-next-line @typescript-eslint/no-unused-vars // eslint-disable-next-line @typescript-eslint/no-unused-vars
const onlineController = new OnlineController(socket, this.io, this.sessionStore); const onlineController = new OnlineController(socket, this.io, this.sessionStore);
const { sessionId, userId, userName } = socket.data; const { sessionId, userId, userName } = socket.data;
const isExistSocketData = !!(sessionId && userId && userName); const isExistSocketData = !!(sessionId && userId && userName);
if (isExistSocketData) {
socket.join(userId);
}
/** /**
* Send session to client * Send session to client
*/ */
socket.emit('user:session', { sessionId: socket.data.sessionId, userId: socket.data.userId }); 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 * Destroy session on user logout request
*/ */

View File

@ -72,7 +72,13 @@ class RedisSessionStore {
*/ */
public async getOnlineSessions() { public async getOnlineSessions() {
const sessions = await this.getSessions(); 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<string, { userId: string; userName: string }>();
sessions.forEach(s => {
if (s?.connected === true) {
online.set(s.userId, s);
}
});
return Array.from(online); return Array.from(online);
} }