-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
127ae5d
commit c6feb82
Showing
4 changed files
with
47 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,65 +1,50 @@ | ||
/* eslint-disable @typescript-eslint/no-explicit-any */ | ||
import { Server } from 'socket.io'; | ||
import type { Server as HTTPServer } from 'http'; | ||
import { dev } from '$app/environment'; | ||
|
||
let io: Server | null = null; | ||
|
||
export const initSocketIO = (httpServer: HTTPServer) => { | ||
if (!io) { | ||
console.log('Initializing Socket.IO server...'); | ||
io = new Server(httpServer, { | ||
path: '/socket.io', | ||
cors: { | ||
origin: ['https://youtubepedia.barron.agency', 'http://localhost:3000'], | ||
methods: ['GET', 'POST'], | ||
credentials: true | ||
}, | ||
transports: ['websocket', 'polling'] | ||
}); | ||
|
||
io.on('connection', (socket) => { | ||
console.log('Client connected', socket.id); | ||
|
||
const userId = socket.handshake.auth.userId; | ||
if (userId) { | ||
socket.join(userId); | ||
console.log(`User ${userId} joined their room`); | ||
let initializationPromise: Promise<Server> | null = null; | ||
|
||
export const initSocketIO = (httpServer: HTTPServer): Promise<Server> => { | ||
if (!initializationPromise) { | ||
initializationPromise = new Promise((resolve) => { | ||
if (!io) { | ||
console.log('Initializing Socket.IO server...'); | ||
io = new Server(httpServer, { | ||
path: '/socket.io', | ||
cors: { | ||
origin: dev ? ['http://localhost:3000'] : ['https://youtubepedia.barron.agency'], | ||
methods: ['GET', 'POST'], | ||
credentials: true | ||
}, | ||
transports: ['websocket', 'polling'] | ||
}); | ||
|
||
io.on('connection', (socket) => { | ||
console.log('Client connected', socket.id); | ||
|
||
const userId = socket.handshake.auth.userId; | ||
if (userId) { | ||
socket.join(userId); | ||
console.log(`User ${userId} joined their room`); | ||
} | ||
|
||
socket.on('disconnect', () => { | ||
console.log('Client disconnected', socket.id); | ||
}); | ||
}); | ||
|
||
console.log('Socket.IO server initialized successfully'); | ||
} | ||
|
||
socket.on('disconnect', () => { | ||
console.log('Client disconnected', socket.id); | ||
}); | ||
resolve(io); | ||
}); | ||
|
||
console.log('Socket.IO server initialized successfully'); | ||
} | ||
return io; | ||
return initializationPromise; | ||
}; | ||
|
||
export const getIO = (): Server => { | ||
if (!io) { | ||
console.warn('Socket.IO accessed before initialization, waiting...'); | ||
return new Proxy({} as Server, { | ||
get: (target, prop) => { | ||
if (prop === 'to' || prop === 'emit') { | ||
return (...args: any[]) => { | ||
setTimeout(() => { | ||
const realIO = getIO(); | ||
if (realIO && prop in realIO) { | ||
(realIO[prop as keyof Server] as any)(...args); | ||
} | ||
}, 100); | ||
}; | ||
} | ||
return () => { | ||
console.warn('Socket.IO method called before initialization'); | ||
}; | ||
} | ||
}); | ||
throw new Error('Socket.IO has not been initialized'); | ||
} | ||
return io; | ||
}; | ||
|
||
export const isSocketInitialized = (): boolean => { | ||
return io !== null; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters