From a1e62932f20fd87ef211ff10226868fa8bcd07c3 Mon Sep 17 00:00:00 2001 From: Bit-Barron Date: Sat, 2 Nov 2024 19:38:25 +0100 Subject: [PATCH] asd --- docker-compose.yml | 12 ++++++++++ src/lib/server/socket.ts | 49 +++++++++++++++++++++++----------------- src/lib/socket.ts | 17 +++++++++++--- 3 files changed, 54 insertions(+), 24 deletions(-) diff --git a/docker-compose.yml b/docker-compose.yml index 138c1c2..9b5eaa1 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -21,10 +21,22 @@ services: - proxy 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=true' - 'traefik.http.routers.youtubepedia.tls.certresolver=letsencrypt' + # Middleware for websocket + - 'traefik.http.middlewares.sio.headers.customrequestheaders.X-Forwarded-Host=youtubepedia.barron.agency' + - 'traefik.http.middlewares.sio.headers.customrequestheaders.X-Forwarded-Proto=https' + - 'traefik.http.middlewares.sio.headers.customrequestheaders.X-Forwarded-Port=443' + # Service configuration - 'traefik.http.services.youtubepedia.loadbalancer.server.port=3000' + # WebSocket specific configuration + - 'traefik.http.routers.youtubepedia.middlewares=sio' + - 'traefik.http.middlewares.youtubepredia-compress.compress=true' + - 'traefik.http.routers.youtubepedia.middlewares=youtubepredia-compress' + networks: proxy: external: false diff --git a/src/lib/server/socket.ts b/src/lib/server/socket.ts index ac7545a..0a89914 100644 --- a/src/lib/server/socket.ts +++ b/src/lib/server/socket.ts @@ -2,28 +2,12 @@ import { Server } from 'socket.io'; import type { Server as HTTPServer } from 'http'; import { dev } from '$app/environment'; +import type { ServerOptions } from 'socket.io'; -// Access the global io instance declare global { var __socketio: Server | null; } -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; - } -}; - export const initSocketIO = (httpServer: HTTPServer): void => { if (!global.__socketio) { console.log('Initializing Socket.IO server...'); @@ -34,11 +18,27 @@ export const initSocketIO = (httpServer: HTTPServer): void => { methods: ['GET', 'POST'], credentials: true }, - transports: ['websocket', 'polling'] + transports: ['polling', 'websocket'], + allowUpgrades: true, + upgradeTimeout: 10000, + pingInterval: 10000, + pingTimeout: 5000, + cookie: { + name: 'io', + path: '/', + httpOnly: true, + sameSite: 'strict' + }, + allowEIO3: true, + connectTimeout: 45000 + } as Partial); + + io.engine.on('connection_error', (err) => { + console.log('Connection error:', err); }); io.on('connection', (socket) => { - console.log('Client connected', socket.id); + console.log('Client connected', socket.id, 'transport:', socket.conn.transport.name); const userId = socket.handshake.auth.userId; if (userId) { @@ -46,8 +46,8 @@ export const initSocketIO = (httpServer: HTTPServer): void => { console.log(`User ${userId} joined their room`); } - socket.on('disconnect', () => { - console.log('Client disconnected', socket.id); + socket.on('disconnect', (reason) => { + console.log('Client disconnected', socket.id, 'reason:', reason); }); }); @@ -55,3 +55,10 @@ export const initSocketIO = (httpServer: HTTPServer): void => { console.log('Socket.IO server initialized successfully'); } }; + +export const getIO = (): Server => { + if (!global.__socketio) { + throw new Error('Socket.IO not initialized'); + } + return global.__socketio; +}; diff --git a/src/lib/socket.ts b/src/lib/socket.ts index 7e1fb80..3811d38 100644 --- a/src/lib/socket.ts +++ b/src/lib/socket.ts @@ -9,18 +9,29 @@ export const initSocket = (userId: string): Socket | null => { reconnection: true, reconnectionDelay: 1000, reconnectionAttempts: 5, - transports: ['websocket', 'polling'] + transports: ['polling', 'websocket'], // Start with polling first + upgrade: true, + rememberUpgrade: true, + timeout: 10000, + forceNew: true }); socket.on('connect', () => { - console.log('Connected to socket server'); + console.log('Connected to socket server with transport:', socket.io.engine.transport.name); }); socket.on('connect_error', (error) => { console.error('Socket connection error:', error); }); + socket.on('disconnect', (reason) => { + console.log('Disconnected:', reason); + if (reason === 'io server disconnect') { + socket.connect(); + } + }); + return socket; } return null; -}; \ No newline at end of file +};