const whatsappEvents = require('../emitter'); module.exports = () => { const sessions = new Map(); const createSession = (sessionId, wsObj) => { const { status, ...ws } = wsObj; // 不管status, 有session就是已连接 sessions.set(ws, sessionId); whatsappEvents.emit('connection:connect', ws); return { sessionId }; }; const getSession = sessionId => { for (const [ws, storedSessionId] of sessions) { if (storedSessionId === sessionId) { return ws; } } return null; }; const removeSession = wsChannelId => { for (const [ws, storedSessionId] of sessions) { if (ws.channelId === wsChannelId) { sessions.delete(ws); } } }; return { createSession, getSession, removeSession, sessions, }; };