mirror of
https://github.com/sergeyyarkov/educt-server.git
synced 2026-10-11 10:19:20 +03:00
refactor: user online
This commit is contained in:
parent
452d7d9557
commit
953283247b
@ -9,6 +9,7 @@
|
||||
## Added
|
||||
|
||||
- About description for user and `last_login` column date
|
||||
- Updating info api route of authorized user
|
||||
|
||||
### Fix
|
||||
|
||||
|
||||
@ -23,9 +23,8 @@ export default class OnlineController {
|
||||
this.socket.on('disconnect', reason => this.onDisconnected(reason));
|
||||
}
|
||||
|
||||
public sendOnlineCount(online: number) {
|
||||
this.socket.emit('user:online', online);
|
||||
this.socket.broadcast.emit('user:online', online);
|
||||
public sendOnline(online: string[]) {
|
||||
this.io.sockets.emit('user:online', online);
|
||||
}
|
||||
|
||||
public setUserOnline() {
|
||||
@ -37,8 +36,8 @@ export default class OnlineController {
|
||||
*/
|
||||
this.sessionStore
|
||||
.saveSession(sessionId, { userId, userName, connected: true })
|
||||
.then(() => this.sessionStore.getOnlineSessionsCount())
|
||||
.then(online => this.sendOnlineCount(online));
|
||||
.then(() => this.sessionStore.getOnlineSessions())
|
||||
.then(online => this.sendOnline(online));
|
||||
}
|
||||
}
|
||||
|
||||
@ -54,7 +53,7 @@ export default class OnlineController {
|
||||
*/
|
||||
this.sessionStore
|
||||
.saveSession(sessionId, { userId, userName, connected: false })
|
||||
.then(() => this.sessionStore.getOnlineSessionsCount())
|
||||
.then(() => this.sessionStore.getOnlineSessions())
|
||||
.then(online => this.socket.broadcast.emit('user:online', online));
|
||||
}
|
||||
}
|
||||
|
||||
@ -20,7 +20,7 @@ export default class StatService {
|
||||
`
|
||||
);
|
||||
|
||||
const online = await Ws.sessionStore.getOnlineSessionsCount();
|
||||
const online = await Ws.sessionStore.getOnlineSessions();
|
||||
|
||||
return {
|
||||
success: true,
|
||||
@ -28,7 +28,7 @@ export default class StatService {
|
||||
message: 'Fetched stat.',
|
||||
data: {
|
||||
...count.rows[0],
|
||||
online,
|
||||
online: online.length,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
@ -19,7 +19,7 @@ export interface ServerToClientEvents {
|
||||
*/
|
||||
'user:session': (data: { sessionId: string | undefined; userId: string | undefined }) => void;
|
||||
'user:connected': (data: { userId: string; userName: string }) => void;
|
||||
'user:online': (data: number) => void;
|
||||
'user:online': (data: string[]) => void;
|
||||
}
|
||||
|
||||
export interface ClientToServerEvents {
|
||||
|
||||
@ -57,7 +57,7 @@ class RedisSessionStore {
|
||||
const keys = await this.scanSessionKeys();
|
||||
const commands = Array.from(keys).map(key => {
|
||||
// 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'];
|
||||
});
|
||||
|
||||
@ -70,11 +70,11 @@ class RedisSessionStore {
|
||||
/**
|
||||
* Returns the unique sessions with connected flag `true`
|
||||
*/
|
||||
public async getOnlineSessionsCount() {
|
||||
public async getOnlineSessions() {
|
||||
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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user