Skip to content

Commit

Permalink
clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
Bit-Barron committed Nov 2, 2024
1 parent d34248e commit ad78846
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 39 deletions.
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
7 changes: 4 additions & 3 deletions src/lib/socket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ 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, {
const socketUrl = `${window.location.protocol}//${window.location.hostname}:3001`;

const socket = io(socketUrl, {
auth: { userId },
reconnection: true,
reconnectionDelay: 1000,
reconnectionAttempts: 5,
path: '/socket.io' // Muss mit Server-Path übereinstimmen
path: '/socket.io'
});

socket.on('connect', () => {
Expand Down
28 changes: 4 additions & 24 deletions vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,10 @@ const webSocketServer = {

const io = new Server(server.httpServer, {
cors: {
origin: ['http://localhost:3000', 'https://youtubepedia.barron.agency'],
origin: '*',
methods: ['GET', 'POST'],
credentials: true,
allowedHeaders: ['Content-Type', 'Authorization']
},
path: '/socket.io',
transports: ['websocket', 'polling'],
pingTimeout: 60000,
pingInterval: 25000
credentials: true
}
} as Partial<ServerOptions>);

io.on('connection', (socket) => {
Expand All @@ -31,31 +26,16 @@ const webSocketServer = {
console.log(`User ${userId} joined their room`);
}

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

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

io.engine?.on('connection_error', (err) => {
console.error('Connection error:', err);
});
}
};

export default defineConfig({
plugins: [sveltekit(), webSocketServer],
server: {
port: 3000,
host: true,
cors: true,
proxy: {
'/socket.io': {
target: 'ws://localhost:3000'
}
}
port: 3000
}
});

0 comments on commit ad78846

Please sign in to comment.