| import { Server as SocketServer } from 'socket.io'; |
| import { Server as HttpServer } from 'http'; |
|
|
| let io: SocketServer | null = null; |
|
|
| export function initSocket(server: any) { |
| io = new SocketServer(server, { |
| cors: { |
| origin: "*", |
| methods: ["GET", "POST"] |
| }, |
| transports: ['websocket', 'polling'] |
| }); |
|
|
| io.on('connection', (socket) => { |
| console.log(`[Socket] Client connected: ${socket.id}`); |
| |
| socket.on('subscribe', (userId: string) => { |
| console.log(`[Socket] User ${userId} subscribed to updates`); |
| socket.join(`user:${userId}`); |
| }); |
|
|
| socket.on('disconnect', () => { |
| console.log(`[Socket] Client disconnected: ${socket.id}`); |
| }); |
| }); |
|
|
| return io; |
| } |
|
|
| export function getIO() { |
| if (!io) { |
| console.warn("[Socket] IO not initialized!"); |
| } |
| return io; |
| } |
|
|
| export function emitToUser(userId: string, event: string, data: any) { |
| if (io) { |
| io.to(`user:${userId}`).emit(event, data); |
| } |
| } |
|
|