11import { chatService } from "../chat/services/chat.service" ;
22import { AppError } from "../errors/AppError" ;
3- import { Server as SocketIOServer , Socket } from "socket.io" ;
3+ import { Server as SocketIOServer , Socket , Namespace } from "socket.io" ;
44import { Server as HttpServer } from "http" ;
55import jwt from "jsonwebtoken" ;
66import prisma from "../config/prisma" ;
77import e from "express" ;
88
9- const setupSocketEvents = ( io : SocketIOServer , socket : Socket ) => {
9+ const setupSocketEvents = ( io : Namespace | SocketIOServer , socket : Socket ) => {
1010 const userId = socket . data . userId ; // 미들웨어에서 설정한 id
1111
1212 // 방 입장
@@ -88,8 +88,11 @@ export const initSocket = (httpServer: HttpServer) => {
8888 }
8989 } ) ;
9090
91+ // 채팅 전용 네임스페이스 생성
92+ const chatNamespace = io . of ( "/ws/chat" ) ;
93+
9194 // 인증 미들웨어
92- io . use ( async ( socket , next ) => {
95+ chatNamespace . use ( async ( socket , next ) => {
9396
9497 try {
9598 const token = socket . handshake . auth ?. token as string | undefined ;
@@ -127,8 +130,8 @@ export const initSocket = (httpServer: HttpServer) => {
127130 } ) ;
128131
129132 // 연결 시작
130- io . on ( "connection" , ( socket : Socket ) => {
131- console . log ( "새로운 소켓 연결:" , socket . id ) ;
132- setupSocketEvents ( io , socket ) ;
133+ chatNamespace . on ( "connection" , ( socket : Socket ) => {
134+ console . log ( "채팅 네임스페이스 연결 성공 :" , socket . id ) ;
135+ setupSocketEvents ( chatNamespace , socket ) ;
133136 } ) ;
134137} ;
0 commit comments