/** * Server-Sent Events (SSE) 서비스 * 실시간 메일 수신 알림 */ const clients = new Set(); /** * 새 클라이언트 연결 추가 */ exports.addClient = (res) => { clients.add(res); console.log(`[SSE] 클라이언트 연결: 총 ${clients.size}명`); }; /** * 클라이언트 연결 제거 */ exports.removeClient = (res) => { clients.delete(res); console.log(`[SSE] 클라이언트 연결 해제: 총 ${clients.size}명`); }; /** * 모든 클라이언트에게 새 메일 알림 전송 */ exports.notifyNewMail = (mailData) => { const message = JSON.stringify({ type: "new-mail", data: { from: mailData.from, to: mailData.to, subject: mailData.subject, date: mailData.date, }, }); console.log(`[SSE] 새 메일 알림 전송: ${clients.size}명에게`); clients.forEach((client) => { try { client.write(`data: ${message}\n\n`); } catch (error) { console.error("[SSE] 메시지 전송 실패:", error); clients.delete(client); } }); };