-
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
43f949f
commit bf22a81
Showing
6 changed files
with
119 additions
and
79 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
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,50 +1,57 @@ | ||
/* eslint-disable no-var */ | ||
import { Server } from 'socket.io'; | ||
import type { Server as HTTPServer } from 'http'; | ||
import { dev } from '$app/environment'; | ||
|
||
let io: Server | null = null; | ||
let initializationPromise: Promise<Server> | null = null; | ||
// Access the global io instance | ||
declare global { | ||
var __socketio: Server | 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); | ||
export const getIO = (): Server => { | ||
if (dev) { | ||
// In development, use the Vite-initialized socket | ||
if (!global.__socketio) { | ||
throw new Error('Socket.IO not initialized yet'); | ||
} | ||
return global.__socketio; | ||
} else { | ||
// In production, initialize normally | ||
if (!global.__socketio) { | ||
throw new Error('Socket.IO not initialized yet'); | ||
} | ||
return global.__socketio; | ||
} | ||
}; | ||
|
||
const userId = socket.handshake.auth.userId; | ||
if (userId) { | ||
socket.join(userId); | ||
console.log(`User ${userId} joined their room`); | ||
} | ||
export const initSocketIO = (httpServer: HTTPServer): void => { | ||
if (!global.__socketio) { | ||
console.log('Initializing Socket.IO server...'); | ||
const 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'] | ||
}); | ||
|
||
socket.on('disconnect', () => { | ||
console.log('Client disconnected', socket.id); | ||
}); | ||
}); | ||
io.on('connection', (socket) => { | ||
console.log('Client connected', socket.id); | ||
|
||
console.log('Socket.IO server initialized successfully'); | ||
const userId = socket.handshake.auth.userId; | ||
if (userId) { | ||
socket.join(userId); | ||
console.log(`User ${userId} joined their room`); | ||
} | ||
resolve(io); | ||
|
||
socket.on('disconnect', () => { | ||
console.log('Client disconnected', socket.id); | ||
}); | ||
}); | ||
} | ||
return initializationPromise; | ||
}; | ||
|
||
export const getIO = (): Server => { | ||
if (!io) { | ||
throw new Error('Socket.IO has not been initialized'); | ||
global.__socketio = io; | ||
console.log('Socket.IO server initialized successfully'); | ||
} | ||
return io; | ||
}; |
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
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