|
| 1 | +import { Server, Socket } from 'socket.io'; |
| 2 | +import { Masterchat, stringify } from 'masterchat'; |
| 3 | + |
| 4 | +let masterchat: Masterchat | null = null; |
| 5 | +let activeSockets: Map<string, Socket> = new Map(); |
| 6 | + |
| 7 | +const HEARTBEAT_INTERVAL = 30000; // 30 seconds |
| 8 | +const HEARTBEAT_TIMEOUT = 60000; // 60 seconds |
| 9 | + |
| 10 | +const setupChatSocket = (io: Server) => { |
| 11 | + const startMasterchat = async (videoUrl: string) => { |
| 12 | + console.log(`Attempting to initialize Masterchat for video: ${videoUrl}`); |
| 13 | + if (masterchat) { |
| 14 | + console.log('Stopping existing Masterchat instance'); |
| 15 | + await masterchat.stop(); |
| 16 | + } |
| 17 | + |
| 18 | + try { |
| 19 | + console.log('Creating new Masterchat instance'); |
| 20 | + masterchat = await Masterchat.init(videoUrl); |
| 21 | + console.log('Masterchat initialized successfully'); |
| 22 | + |
| 23 | + const chats = masterchat.iter().filter((action) => action.type === "addChatItemAction"); |
| 24 | + |
| 25 | + for await (const chat of chats) { |
| 26 | + console.log('Emitting chat message:', chat); |
| 27 | + io.emit('chatMessage', { |
| 28 | + author: { |
| 29 | + name: chat.authorName, |
| 30 | + channelId: chat.authorChannelId, |
| 31 | + }, |
| 32 | + message: stringify(chat.message), |
| 33 | + timestamp: new Date(), |
| 34 | + }); |
| 35 | + } |
| 36 | + } catch (error) { |
| 37 | + console.error('Error initializing Masterchat:', error); |
| 38 | + io.emit('error', 'Failed to start listening for live chat messages'); |
| 39 | + masterchat = null; |
| 40 | + } |
| 41 | + }; |
| 42 | + |
| 43 | + io.on('connection', (socket: Socket) => { |
| 44 | + console.log(`New client connected: ${socket.id}`); |
| 45 | + activeSockets.set(socket.id, socket); |
| 46 | + console.log(`Active connections: ${activeSockets.size}`); |
| 47 | + |
| 48 | + socket.on('switchChannel', (videoUrl: string) => { |
| 49 | + startMasterchat(videoUrl); |
| 50 | + }); |
| 51 | + |
| 52 | + socket.on('disconnect', (reason) => { |
| 53 | + console.log(`Client disconnected: ${socket.id}, Reason: ${reason}`); |
| 54 | + activeSockets.delete(socket.id); |
| 55 | + console.log(`Active connections: ${activeSockets.size}`); |
| 56 | + }); |
| 57 | + |
| 58 | + // Implement heartbeat |
| 59 | + let heartbeatTimeout: NodeJS.Timeout; |
| 60 | + const resetHeartbeat = () => { |
| 61 | + if (heartbeatTimeout) clearTimeout(heartbeatTimeout); |
| 62 | + heartbeatTimeout = setTimeout(() => { |
| 63 | + console.log(`Heartbeat timeout for client: ${socket.id}`); |
| 64 | + socket.disconnect(true); |
| 65 | + }, HEARTBEAT_TIMEOUT); |
| 66 | + }; |
| 67 | + |
| 68 | + socket.on('heartbeat', () => { |
| 69 | + resetHeartbeat(); |
| 70 | + }); |
| 71 | + |
| 72 | + resetHeartbeat(); |
| 73 | + }); |
| 74 | + |
| 75 | + // Periodic cleanup of stale connections |
| 76 | + setInterval(() => { |
| 77 | + for (const [id, socket] of activeSockets) { |
| 78 | + if (!socket.connected) { |
| 79 | + activeSockets.delete(id); |
| 80 | + socket.disconnect(true); |
| 81 | + console.log(`Removed stale connection: ${id}`); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + console.log(`Active connections after cleanup: ${activeSockets.size}`); |
| 86 | + }, HEARTBEAT_INTERVAL); |
| 87 | +}; |
| 88 | + |
| 89 | +export { setupChatSocket }; |
0 commit comments