Skip to content

Commit

Permalink
a
Browse files Browse the repository at this point in the history
  • Loading branch information
Bit-Barron committed Nov 2, 2024
1 parent ea87cf8 commit 7edacd5
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 31 deletions.
28 changes: 17 additions & 11 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,29 @@ services:
networks:
- proxy
ports:
- '3001:3001'
- '3001:3001' # Socket.IO port
- '3000:3000' # Main app port
labels:
- 'traefik.enable=true'
# HTTP Router
# Main application 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'
# 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.middlewares.youtubepedia-ws.headers.customrequestheaders.Upgrade=websocket'
- 'traefik.http.routers.youtubepedia-ws.middlewares=youtubepedia-ws'

# Socket.IO specific router
- 'traefik.http.routers.youtubepedia-socket.rule=Host(`youtubepedia.barron.agency`) && PathPrefix(`/socket.io`)'
- 'traefik.http.routers.youtubepedia-socket.entrypoints=websecure'
- 'traefik.http.routers.youtubepedia-socket.service=youtubepedia-socket-service'
- 'traefik.http.routers.youtubepedia-socket.tls.certresolver=letsencrypt'

# Socket.IO service definition
- 'traefik.http.services.youtubepedia-socket-service.loadbalancer.server.port=3001'

# WebSocket middleware
- 'traefik.http.middlewares.ws-middleware.headers.customrequestheaders.Connection=Upgrade'
- 'traefik.http.middlewares.ws-middleware.headers.customrequestheaders.Upgrade=websocket'
- 'traefik.http.routers.youtubepedia-socket.middlewares=ws-middleware'

networks:
proxy:
Expand Down
16 changes: 4 additions & 12 deletions src/lib/server/socket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,13 @@ 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'],
pingTimeout: 60000,
pingInterval: 25000,
connectTimeout: 45000,
allowEIO3: true
transports: ['websocket', 'polling']
});

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

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
11 changes: 3 additions & 8 deletions src/lib/socket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,21 @@ import { io, type Socket } from 'socket.io-client';

export const initSocket = (userId: string): Socket | null => {
if (browser) {
// Kein expliziter Port - nutze den gleichen Port wie die App
const socket = io(window.location.origin, {
auth: { userId },
reconnection: true,
reconnectionDelay: 1000,
reconnectionAttempts: 5,
path: '/socket.io/',
transports: ['websocket', 'polling']
path: '/socket.io' // Muss mit Server-Path übereinstimmen
});

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

socket.on('connect_error', (error) => {
console.error('Socket connection error:', error.message);
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 7edacd5

Please sign in to comment.