refactor: user online

This commit is contained in:
Sergey Yarkov 2022-02-06 22:33:38 +03:00
parent 452d7d9557
commit 953283247b
5 changed files with 13 additions and 13 deletions

View File

@ -9,6 +9,7 @@
## Added ## Added
- About description for user and `last_login` column date - About description for user and `last_login` column date
- Updating info api route of authorized user
### Fix ### Fix

View File

@ -23,9 +23,8 @@ export default class OnlineController {
this.socket.on('disconnect', reason => this.onDisconnected(reason)); this.socket.on('disconnect', reason => this.onDisconnected(reason));
} }
public sendOnlineCount(online: number) { public sendOnline(online: string[]) {
this.socket.emit('user:online', online); this.io.sockets.emit('user:online', online);
this.socket.broadcast.emit('user:online', online);
} }
public setUserOnline() { public setUserOnline() {
@ -37,8 +36,8 @@ export default class OnlineController {
*/ */
this.sessionStore this.sessionStore
.saveSession(sessionId, { userId, userName, connected: true }) .saveSession(sessionId, { userId, userName, connected: true })
.then(() => this.sessionStore.getOnlineSessionsCount()) .then(() => this.sessionStore.getOnlineSessions())
.then(online => this.sendOnlineCount(online)); .then(online => this.sendOnline(online));
} }
} }
@ -54,7 +53,7 @@ export default class OnlineController {
*/ */
this.sessionStore this.sessionStore
.saveSession(sessionId, { userId, userName, connected: false }) .saveSession(sessionId, { userId, userName, connected: false })
.then(() => this.sessionStore.getOnlineSessionsCount()) .then(() => this.sessionStore.getOnlineSessions())
.then(online => this.socket.broadcast.emit('user:online', online)); .then(online => this.socket.broadcast.emit('user:online', online));
} }
} }

View File

@ -20,7 +20,7 @@ export default class StatService {
` `
); );
const online = await Ws.sessionStore.getOnlineSessionsCount(); const online = await Ws.sessionStore.getOnlineSessions();
return { return {
success: true, success: true,
@ -28,7 +28,7 @@ export default class StatService {
message: 'Fetched stat.', message: 'Fetched stat.',
data: { data: {
...count.rows[0], ...count.rows[0],
online, online: online.length,
}, },
}; };
} }

View File

@ -19,7 +19,7 @@ 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: number) => void; 'user:online': (data: string[]) => void;
} }
export interface ClientToServerEvents { export interface ClientToServerEvents {

View File

@ -57,7 +57,7 @@ class RedisSessionStore {
const keys = await this.scanSessionKeys(); const keys = await this.scanSessionKeys();
const commands = Array.from(keys).map(key => { const commands = Array.from(keys).map(key => {
// eslint-disable-next-line @typescript-eslint/no-unused-vars // eslint-disable-next-line @typescript-eslint/no-unused-vars
const [prefix, ...id] = key.split('-'); const [, ...id] = key.split('-');
return ['hmget', id.join('-'), 'userId', 'userName', 'connected']; return ['hmget', id.join('-'), 'userId', 'userName', 'connected'];
}); });
@ -70,11 +70,11 @@ class RedisSessionStore {
/** /**
* Returns the unique sessions with connected flag `true` * Returns the unique sessions with connected flag `true`
*/ */
public async getOnlineSessionsCount() { public async getOnlineSessions() {
const sessions = await this.getSessions(); const sessions = await this.getSessions();
const online = new Set(sessions.filter(s => s?.connected === true).map(s => s?.userId)); const online = new Set(sessions.filter((s): s is SessionDataType => s?.connected === true).map(s => s?.userId));
return online.size; return Array.from(online);
} }
/** /**