Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions src/socket/server.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { chatService } from "../chat/services/chat.service";
import { AppError } from "../errors/AppError";
import { Server as SocketIOServer, Socket } from "socket.io";
import { Server as SocketIOServer, Socket, Namespace } from "socket.io";
import { Server as HttpServer } from "http";
import jwt from "jsonwebtoken";
import prisma from "../config/prisma";
import e from "express";

const setupSocketEvents = (io: SocketIOServer, socket: Socket) => {
const setupSocketEvents = (io: Namespace | SocketIOServer, socket: Socket) => {
const userId = socket.data.userId; // λ―Έλ“€μ›¨μ–΄μ—μ„œ μ„€μ •ν•œ id

// λ°© μž…μž₯
Expand Down Expand Up @@ -88,8 +88,11 @@ export const initSocket = (httpServer: HttpServer) => {
}
});

// μ±„νŒ… μ „μš© λ„€μž„μŠ€νŽ˜μ΄μŠ€ 생성
const chatNamespace = io.of("/ws/chat");

// 인증 미듀웨어
io.use(async (socket, next) => {
chatNamespace.use(async (socket, next) => {

try {
const token = socket.handshake.auth?.token as string | undefined;
Expand Down Expand Up @@ -127,8 +130,8 @@ export const initSocket = (httpServer: HttpServer) => {
});

// μ—°κ²° μ‹œμž‘
io.on("connection", (socket: Socket) => {
console.log("μƒˆλ‘œμš΄ μ†ŒμΌ“ μ—°κ²°:", socket.id);
setupSocketEvents(io, socket);
chatNamespace.on("connection", (socket: Socket) => {
console.log("μ±„νŒ… λ„€μž„μŠ€νŽ˜μ΄μŠ€ μ—°κ²° 성곡:", socket.id);
setupSocketEvents(chatNamespace, socket);
});
};