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 ee014ac commit 9047524
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 31 deletions.
12 changes: 9 additions & 3 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,20 @@ services:
- '3001:3001'
labels:
- 'traefik.enable=true'
# HTTP Router
- 'traefik.http.routers.youtubepedia.rule=Host(`youtubepedia.barron.agency`)'
- 'traefik.http.routers.youtubepedia.entrypoints=websecure'
- 'traefik.http.routers.youtubepedia.tls.certresolver=letsencrypt'
- 'traefik.http.services.youtubepedia.loadbalancer.server.port=3000'
# Updated WebSocket middleware configuration
- 'traefik.http.middlewares.youtubepedia-ws.headers.customrequestheaders.Upgrade=websocket'
# WebSocket Router
- 'traefik.http.routers.youtubepedia-ws.rule=Host(`youtubepedia.barron.agency`) && PathPrefix(`/socket.io`)'
- 'traefik.http.routers.youtubepedia-ws.entrypoints=websecure'
- 'traefik.http.routers.youtubepedia-ws.tls.certresolver=letsencrypt'
- 'traefik.http.services.youtubepedia-ws.loadbalancer.server.port=3001'
# WebSocket Middleware
- 'traefik.http.middlewares.youtubepedia-ws.headers.customrequestheaders.Connection=Upgrade'
- 'traefik.http.routers.youtubepedia.middlewares=youtubepedia-ws'
- 'traefik.http.middlewares.youtubepedia-ws.headers.customrequestheaders.Upgrade=websocket'
- 'traefik.http.routers.youtubepedia-ws.middlewares=youtubepedia-ws'

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

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

// Add error handling
socket.on('error', (error) => {
console.error('Socket error:', error);
});

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

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

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

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

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

socket.on('connect_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
});
}
console.error('Socket connection error:', error);
});

return socket;
Expand Down

0 comments on commit 9047524

Please sign in to comment.