fix: fixed types

This commit is contained in:
Sergey Yarkov 2022-01-30 18:47:40 +03:00
parent 7c678797d7
commit d52cee9f57
2 changed files with 26 additions and 34 deletions

View File

@ -6,7 +6,6 @@ import OpaqueTokenHelper from 'App/Helpers/OpaqueTokenHelper';
import Ws, { ClientToServerEvents, InterServerEvents, ServerToClientEvents, SocketData } from 'App/Services/WsService'; import Ws, { ClientToServerEvents, InterServerEvents, ServerToClientEvents, SocketData } from 'App/Services/WsService';
import { Socket } from 'socket.io'; import { Socket } from 'socket.io';
import { ExtendedError } from 'socket.io/dist/namespace'; import { ExtendedError } from 'socket.io/dist/namespace';
import Logger from '@ioc:Adonis/Core/Logger';
export default class AuthMiddleware { export default class AuthMiddleware {
/** /**
@ -53,8 +52,6 @@ export default class AuthMiddleware {
const session = await Ws.sessionStore.findSession(sessionId); const session = await Ws.sessionStore.findSession(sessionId);
if (session) { if (session) {
Logger.info(`[Ws]: Attacing session from redis session store.`);
socket.data.sessionId = sessionId; socket.data.sessionId = sessionId;
socket.data.userId = session.userId; socket.data.userId = session.userId;
socket.data.userName = session.userName; socket.data.userName = session.userName;
@ -66,7 +63,6 @@ export default class AuthMiddleware {
/** /**
* Parse data from redis string and create new session * Parse data from redis string and create new session
*/ */
Logger.info(`[Ws]: Attaching token data from cookie.`);
const parsedData = JSON.parse(data); const parsedData = JSON.parse(data);
/** /**

View File

@ -1,6 +1,6 @@
import { Server } from 'socket.io';
import Redis from '@ioc:Adonis/Addons/Redis'; import Redis from '@ioc:Adonis/Addons/Redis';
import AdonisServer from '@ioc:Adonis/Core/Server'; import AdonisServer from '@ioc:Adonis/Core/Server';
import { Server } from 'socket.io';
/** /**
* Controllers * Controllers
@ -32,9 +32,9 @@ export interface ClientToServerEvents {
export interface InterServerEvents {} export interface InterServerEvents {}
export interface SocketData { export interface SocketData {
sessionId?: string | undefined; sessionId: string;
userId?: string | undefined; userId: string;
userName?: string | undefined; userName: string;
} }
class WsService { class WsService {
@ -64,23 +64,23 @@ class WsService {
private listen() { private listen() {
this.io.on('connection', socket => { this.io.on('connection', socket => {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const onlineController = new OnlineController(socket); const onlineController = new OnlineController(socket);
const { sessionId, userId, userName } = socket.data;
const isExistSocketData = !!(sessionId && userId && userName);
/** /**
* Set user session and send online count * Set user session and send online count
*/ */
this.sessionStore if (isExistSocketData) {
.saveSession(socket.data.sessionId, { this.sessionStore
sessionId: socket.data.sessionId, .saveSession(sessionId, { userId, userName, connected: true })
userId: socket.data.userId, .then(() => this.sessionStore.getOnlineSessionsCount())
userName: socket.data.userName, .then(online => {
connected: true, socket.emit('user:online', online);
}) socket.broadcast.emit('user:online', online);
.then(async () => { });
const online = await this.sessionStore.getOnlineSessionsCount(); }
socket.emit('user:online', online);
socket.broadcast.emit('user:online', online);
});
/** /**
* Send session to client * Send session to client
@ -96,21 +96,17 @@ class WsService {
}); });
socket.on('disconnect', async () => { socket.on('disconnect', async () => {
const matchingSockets = await this.io.in(socket.data.userId).allSockets(); if (isExistSocketData) {
const isDisconnected = matchingSockets.size === 0; const matchingSockets = await this.io.in(userId).allSockets();
const isDisconnected = matchingSockets.size === 0;
if (isDisconnected) { 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, { this.sessionStore.saveSession(sessionId, { userId, userName, connected: false });
sessionId: socket.data.sessionId, this.sessionStore.getOnlineSessionsCount().then(online => socket.broadcast.emit('user:online', online));
userId: socket.data.userId, }
userName: socket.data.userName,
connected: false,
});
this.sessionStore.getOnlineSessionsCount().then(online => socket.broadcast.emit('user:online', online));
} }
}); });
}); });