Skip to content

Commit df3a042

Browse files
authored
Merge pull request #414 from PromptPlace/bug/#409
Bug/#409
2 parents 2a0983b + 95c16e4 commit df3a042

1 file changed

Lines changed: 9 additions & 6 deletions

File tree

src/socket/server.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import { chatService } from "../chat/services/chat.service";
22
import { AppError } from "../errors/AppError";
3-
import { Server as SocketIOServer, Socket } from "socket.io";
3+
import { Server as SocketIOServer, Socket, Namespace } from "socket.io";
44
import { Server as HttpServer } from "http";
55
import jwt from "jsonwebtoken";
66
import prisma from "../config/prisma";
77
import 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

Comments
 (0)