Skip to content

Commit

Permalink
asd
Browse files Browse the repository at this point in the history
  • Loading branch information
Bit-Barron committed Nov 2, 2024
1 parent 8d12f6c commit ee014ac
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 9 deletions.
7 changes: 4 additions & 3 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,10 @@ services:
- 'traefik.http.routers.youtubepedia.entrypoints=websecure'
- 'traefik.http.routers.youtubepedia.tls.certresolver=letsencrypt'
- 'traefik.http.services.youtubepedia.loadbalancer.server.port=3000'
- 'traefik.http.middlewares.sio.headers.customrequestheaders.Upgrade=websocket'
- 'traefik.http.middlewares.sio.headers.customrequestheaders.Connection=Upgrade'
- 'traefik.http.routers.youtubepedia.middlewares=sio@docker'
# Updated WebSocket middleware configuration
- 'traefik.http.middlewares.youtubepedia-ws.headers.customrequestheaders.Upgrade=websocket'
- 'traefik.http.middlewares.youtubepedia-ws.headers.customrequestheaders.Connection=Upgrade'
- 'traefik.http.routers.youtubepedia.middlewares=youtubepedia-ws'

networks:
proxy:
Expand Down
14 changes: 11 additions & 3 deletions src/lib/server/socket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ export const initSocketIO = (httpServer: HTTPServer): Promise<Server> => {
methods: ['GET', 'POST'],
credentials: true
},
transports: ['websocket', 'polling']
transports: ['websocket', 'polling'],
// Add ping timeout and interval settings
pingTimeout: 60000,
pingInterval: 25000
});

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

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

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

Expand Down
20 changes: 17 additions & 3 deletions src/lib/socket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,35 @@ 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`;
// Determine the socket URL based on the current environment
const socketUrl = browser
? // In production, use the same origin as the web app
window.location.origin
: // In development, use explicit port
`${window.location.protocol}//${window.location.hostname}:3001`;

const socket = io(socketUrl, {
auth: { userId },
reconnection: true,
reconnectionDelay: 1000,
reconnectionAttempts: 5
reconnectionAttempts: 5,
path: '/socket.io' // Explicitly set the socket.io path
});

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);
// Add more detailed error logging
if (error instanceof Error) {
console.error('Error details:', {
message: error.message,
name: error.name,
stack: error.stack
});
}
});

return socket;
Expand Down

0 comments on commit ee014ac

Please sign in to comment.