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 994ae2b commit a1e6293
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 24 deletions.
12 changes: 12 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
49 changes: 28 additions & 21 deletions src/lib/server/socket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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...');
Expand All @@ -34,24 +18,47 @@ 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<ServerOptions>);

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) {
socket.join(userId);
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);
});
});

global.__socketio = io;
console.log('Socket.IO server initialized successfully');
}
};

export const getIO = (): Server => {
if (!global.__socketio) {
throw new Error('Socket.IO not initialized');
}
return global.__socketio;
};
17 changes: 14 additions & 3 deletions src/lib/socket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
};

0 comments on commit a1e6293

Please sign in to comment.