mirror of
https://github.com/sergeyyarkov/educt-server.git
synced 2026-10-11 10:19:20 +03:00
fix: fixed types
This commit is contained in:
parent
7c678797d7
commit
d52cee9f57
@ -6,7 +6,6 @@ import OpaqueTokenHelper from 'App/Helpers/OpaqueTokenHelper';
|
||||
import Ws, { ClientToServerEvents, InterServerEvents, ServerToClientEvents, SocketData } from 'App/Services/WsService';
|
||||
import { Socket } from 'socket.io';
|
||||
import { ExtendedError } from 'socket.io/dist/namespace';
|
||||
import Logger from '@ioc:Adonis/Core/Logger';
|
||||
|
||||
export default class AuthMiddleware {
|
||||
/**
|
||||
@ -53,8 +52,6 @@ export default class AuthMiddleware {
|
||||
const session = await Ws.sessionStore.findSession(sessionId);
|
||||
|
||||
if (session) {
|
||||
Logger.info(`[Ws]: Attacing session from redis session store.`);
|
||||
|
||||
socket.data.sessionId = sessionId;
|
||||
socket.data.userId = session.userId;
|
||||
socket.data.userName = session.userName;
|
||||
@ -66,7 +63,6 @@ export default class AuthMiddleware {
|
||||
/**
|
||||
* Parse data from redis string and create new session
|
||||
*/
|
||||
Logger.info(`[Ws]: Attaching token data from cookie.`);
|
||||
const parsedData = JSON.parse(data);
|
||||
|
||||
/**
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
import { Server } from 'socket.io';
|
||||
import Redis from '@ioc:Adonis/Addons/Redis';
|
||||
import AdonisServer from '@ioc:Adonis/Core/Server';
|
||||
import { Server } from 'socket.io';
|
||||
|
||||
/**
|
||||
* Controllers
|
||||
@ -32,9 +32,9 @@ export interface ClientToServerEvents {
|
||||
export interface InterServerEvents {}
|
||||
|
||||
export interface SocketData {
|
||||
sessionId?: string | undefined;
|
||||
userId?: string | undefined;
|
||||
userName?: string | undefined;
|
||||
sessionId: string;
|
||||
userId: string;
|
||||
userName: string;
|
||||
}
|
||||
|
||||
class WsService {
|
||||
@ -64,23 +64,23 @@ class WsService {
|
||||
|
||||
private listen() {
|
||||
this.io.on('connection', socket => {
|
||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||
const onlineController = new OnlineController(socket);
|
||||
const { sessionId, userId, userName } = socket.data;
|
||||
const isExistSocketData = !!(sessionId && userId && userName);
|
||||
|
||||
/**
|
||||
* Set user session and send online count
|
||||
*/
|
||||
if (isExistSocketData) {
|
||||
this.sessionStore
|
||||
.saveSession(socket.data.sessionId, {
|
||||
sessionId: socket.data.sessionId,
|
||||
userId: socket.data.userId,
|
||||
userName: socket.data.userName,
|
||||
connected: true,
|
||||
})
|
||||
.then(async () => {
|
||||
const online = await this.sessionStore.getOnlineSessionsCount();
|
||||
.saveSession(sessionId, { userId, userName, connected: true })
|
||||
.then(() => this.sessionStore.getOnlineSessionsCount())
|
||||
.then(online => {
|
||||
socket.emit('user:online', online);
|
||||
socket.broadcast.emit('user:online', online);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Send session to client
|
||||
@ -96,22 +96,18 @@ class WsService {
|
||||
});
|
||||
|
||||
socket.on('disconnect', async () => {
|
||||
const matchingSockets = await this.io.in(socket.data.userId).allSockets();
|
||||
if (isExistSocketData) {
|
||||
const matchingSockets = await this.io.in(userId).allSockets();
|
||||
const isDisconnected = matchingSockets.size === 0;
|
||||
|
||||
if (isDisconnected) {
|
||||
/**
|
||||
* Update connection flag of socket to flase
|
||||
* Update connection flag of socket to flase and send online count
|
||||
*/
|
||||
this.sessionStore.saveSession(socket.data.sessionId, {
|
||||
sessionId: socket.data.sessionId,
|
||||
userId: socket.data.userId,
|
||||
userName: socket.data.userName,
|
||||
connected: false,
|
||||
});
|
||||
|
||||
this.sessionStore.saveSession(sessionId, { userId, userName, connected: false });
|
||||
this.sessionStore.getOnlineSessionsCount().then(online => socket.broadcast.emit('user:online', online));
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user