Skip to content

Commit

Permalink
fgrd
Browse files Browse the repository at this point in the history
  • Loading branch information
Bit-Barron committed Nov 2, 2024
1 parent 9047524 commit ea87cf8
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 9 deletions.
16 changes: 12 additions & 4 deletions src/lib/server/socket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,17 @@ export const initSocketIO = (httpServer: HTTPServer): Promise<Server> => {
if (!io) {
console.log('Initializing Socket.IO server...');
io = new Server(httpServer, {
path: '/socket.io',
path: '/socket.io/',
cors: {
origin: dev ? ['http://localhost:3000'] : ['https://youtubepedia.barron.agency'],
methods: ['GET', 'POST'],
credentials: true
},
transports: ['websocket', 'polling']
transports: ['websocket', 'polling'],
pingTimeout: 60000,
pingInterval: 25000,
connectTimeout: 45000,
allowEIO3: true
});

io.on('connection', (socket) => {
Expand All @@ -29,8 +33,12 @@ export const initSocketIO = (httpServer: HTTPServer): Promise<Server> => {
console.log(`User ${userId} joined their room`);
}

socket.on('disconnect', () => {
console.log('Client disconnected', socket.id);
socket.on('error', (error) => {
console.error('Socket error:', error);
});

socket.on('disconnect', (reason) => {
console.log(`Client disconnected (${reason})`, socket.id);
});
});

Expand Down
15 changes: 10 additions & 5 deletions src/lib/socket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,26 @@ import { io, type Socket } from 'socket.io-client';

export const initSocket = (userId: string): Socket | null => {
if (browser) {
const socketUrl = `${window.location.protocol}//${window.location.hostname}:3001`;

const socket = io(socketUrl, {
const socket = io(window.location.origin, {
auth: { userId },
reconnection: true,
reconnectionDelay: 1000,
reconnectionAttempts: 5
reconnectionAttempts: 5,
path: '/socket.io/',
transports: ['websocket', 'polling']
});

socket.on('connect', () => {
console.log('Connected to socket server');
});

socket.on('connect_error', (error) => {
console.error('Socket connection error:', error);
console.error('Socket connection error:', error.message);
console.error('Error details:', {
message: error.message,
name: error.name,
stack: error.stack
});
});

return socket;
Expand Down

0 comments on commit ea87cf8

Please sign in to comment.