| | import express from 'express'; |
| | import { createServer } from 'http'; |
| | import { Server } from 'socket.io'; |
| | import cors from 'cors'; |
| | import path from 'path'; |
| | import { fileURLToPath } from 'url'; |
| | import { RoomManager } from './rooms/RoomManager.js'; |
| | import { setupSocketHandlers } from './socket/handlers.js'; |
| | import type { ServerToClientEvents, ClientToServerEvents } from '../../shared/types.js'; |
| |
|
| | const __filename = fileURLToPath(import.meta.url); |
| | const __dirname = path.dirname(__filename); |
| |
|
| | const app = express(); |
| | app.use(cors()); |
| |
|
| | |
| | |
| | const clientPath = process.env.NODE_ENV === 'production' |
| | ? path.join(process.cwd(), 'client/dist') |
| | : path.join(__dirname, '../../client/dist'); |
| | app.use(express.static(clientPath)); |
| |
|
| | const httpServer = createServer(app); |
| |
|
| | |
| | const allowedOrigins = process.env.NODE_ENV === 'production' |
| | ? true |
| | : ['http://localhost:5173', 'http://localhost:3000', 'http://127.0.0.1:5173']; |
| |
|
| | const io = new Server<ClientToServerEvents, ServerToClientEvents>(httpServer, { |
| | cors: { |
| | origin: allowedOrigins, |
| | methods: ['GET', 'POST'], |
| | }, |
| | }); |
| |
|
| | const roomManager = new RoomManager(); |
| |
|
| | io.on('connection', (socket) => { |
| | console.log(`Player connected: ${socket.id}`); |
| | setupSocketHandlers(io, socket, roomManager); |
| | |
| | socket.on('disconnect', () => { |
| | console.log(`Player disconnected: ${socket.id}`); |
| | roomManager.handleDisconnect(socket.id, io); |
| | }); |
| | }); |
| |
|
| | |
| | app.get('*', (req, res) => { |
| | res.sendFile(path.join(clientPath, 'index.html')); |
| | }); |
| |
|
| | const PORT = process.env.PORT || 3001; |
| |
|
| | httpServer.listen(PORT, () => { |
| | console.log(`🎮 Mummy Card Game Server running on port ${PORT}`); |
| | }); |
| |
|