mirror of
https://github.com/linux-odyssey/linux-odyssey.git
synced 2026-11-06 03:20:13 +08:00
refactor: clean up unused code
This commit is contained in:
1 parent
21ff710f83
commit
156abc044a
3 files changed
+8
-91
No files matched your search
@@ -2,7 +2,7 @@ import { Server } from 'socket.io'
|
||||
import type { Socket } from 'socket.io'
|
||||
import validator from 'validator'
|
||||
import type { Server as HttpServer } from 'http'
|
||||
import { ISession, Session } from '../../../packages/models'
|
||||
import { Session } from '../../../packages/models'
|
||||
import SessionMiddleware from '../middleware/session.js'
|
||||
import logger from '../utils/logger.js'
|
||||
|
||||
@@ -34,11 +34,11 @@ export function pushToSession(
|
||||
}
|
||||
}
|
||||
|
||||
// eslint-disable-next-line no-unused-vars
|
||||
async function connectContainer(socket: Socket, next: (err?: Error) => void) {
|
||||
async function onConnect(socket: Socket) {
|
||||
const user = (socket.request as any).session?.passport?.user
|
||||
if (!user) {
|
||||
next(new Error('User not found.'))
|
||||
socket.emit('error', 'User not found.')
|
||||
socket.disconnect()
|
||||
return
|
||||
}
|
||||
const { sessionId } = socket.handshake.query
|
||||
@@ -48,27 +48,18 @@ async function connectContainer(socket: Socket, next: (err?: Error) => void) {
|
||||
!validator.isMongoId(sessionId)
|
||||
) {
|
||||
logger.warn('Invalid Session ID.', user.username, sessionId)
|
||||
next(new Error('Invalid Session ID.'))
|
||||
socket.emit('error', 'Invalid Session ID.')
|
||||
socket.disconnect()
|
||||
return
|
||||
}
|
||||
|
||||
const session = await Session.findById(sessionId)
|
||||
if (!session) {
|
||||
next(new Error('Session not found.'))
|
||||
socket.emit('error', 'Session not found.')
|
||||
socket.disconnect()
|
||||
return
|
||||
}
|
||||
|
||||
socket.data.context = {
|
||||
session,
|
||||
}
|
||||
next()
|
||||
}
|
||||
|
||||
function onConnect(socket: Socket) {
|
||||
const { session } = socket.data.context as {
|
||||
session: ISession
|
||||
}
|
||||
|
||||
const socketCallback = (event: string, ...data: any[]) => {
|
||||
socket.emit(event, ...data)
|
||||
}
|
||||
@@ -84,7 +75,6 @@ export default (server: HttpServer) => {
|
||||
const io = new Server(server)
|
||||
|
||||
io.engine.use(SessionMiddleware)
|
||||
io.use(connectContainer)
|
||||
|
||||
io.on('connection', onConnect)
|
||||
io.on('connect_error', (err) => {
|
||||
|
||||
Reference in new issue
Block a user