JavaScript

map에서 set 까진 잘 되는데 이 값을 불러오려니 값이 없습니다 제발 도와주세요

JD

JDK#9650
답변 대기중
30 XP

소스 코드입니다 ㅜㅜ

const { WebSocketServer } = require("ws");
const clients = new Map();

function WSSON(Info) {
    const wss = new WebSocketServer({ port: 8023 });
    console.log("[✅] WebSocketServer 준비 완료");

    wss.on("connection", (ws, request) => {
        const ip = request.headers['x-forwarded-for'] || request.connection.remoteAddress;
        console.log(`[💻] - [${ip}] 접속`);

        ws.on('error', (error) => {
            console.log(`[💻] - [${ip}] 연결 에러: ${error}`);
        });

        ws.on('close', () => {
            clients.delete(ws);
            console.log(`[💻] - [${ip}] WebSocket 연결 종료`);
        });

        clients.set(ws, ws._protocol);
    });
}

function WSS_Send(Info, targetUserId) {
    const message = JSON.stringify(Info);

    for (const [ws, userid] of clients.entries()) {
        if (userid === targetUserId) {
            ws.send(message);
        }
    }
}

function WSS_Ready(data) {
    const message = JSON.stringify({ type: 'Ready' });

    for (const [ws, userid] of clients.entries()) {
        if (userid === data?.userid) {
            ws.send(message);
        }
    }
}

module.exports = {
    WSSON,
    WSS_Send,
    WSS_Ready
};

이 문제 하나로 27시간째 멈춰있습니다 ㅜㅜ 제발 도와주세요


불러오는 중...