아이디 정의

This commit is contained in:
2025-09-20 12:05:29 +09:00
parent 31197bcd1b
commit c5d116d73a
2 changed files with 9 additions and 26 deletions

View File

@@ -8,17 +8,20 @@ const wss = new WebSocketServer({ server });
const peers = new Map();
wss.on('connection', (ws) => {
const id = Math.random().toString(36).substring(2, 9);
peers.set(id, ws);
console.log(`${id} connected!`);
ws.send(JSON.stringify({ type: 'your-id', id }));
// const id = Math.random().toString(36).substring(2, 9);
ws.on('message', (data) => {
const msg = JSON.parse(data);
if (msg.target && peers.has(msg.target)) {
if(msg.type === 'login') {
peers.set(msg.sabun, ws);
ws.send(JSON.stringify({ type: 'your-id',id: msg.sabun }));
console.log(`${msg.sabun} connected!`);
} else if (msg.target && peers.has(msg.target)) {
peers.get(msg.target).send(JSON.stringify(msg));
}
});
ws.on('close', () => peers.delete(id));
ws.on('close', () => peers.delete(msg.sabun));
});