-
Notifications
You must be signed in to change notification settings - Fork 0
/
vite.config.ts
52 lines (43 loc) · 1.28 KB
/
vite.config.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
/* eslint-disable @typescript-eslint/no-explicit-any */
import { sveltekit } from '@sveltejs/kit/vite';
import { defineConfig } from 'vite';
import type { ViteDevServer } from 'vite';
import { Server } from 'socket.io';
import type { ServerOptions } from 'socket.io';
let io: Server | null = null;
const webSocketServer = {
name: 'webSocketServer',
configureServer(server: ViteDevServer) {
if (!server.httpServer) return;
if (!io) {
console.log('Initializing Socket.IO server in Vite...');
io = new Server(server.httpServer, {
cors: {
origin: '*',
methods: ['GET', 'POST'],
credentials: true
}
} as Partial<ServerOptions>);
// Store the io instance on the global object for access from other files
(global as any).__socketio = io;
io.on('connection', (socket) => {
console.log('Client connected', socket.id);
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);
});
});
console.log('Socket.IO server initialized successfully in Vite');
}
}
};
export default defineConfig({
plugins: [sveltekit(), webSocketServer],
server: {
port: 3000
}
});