Spaces:
Build error
Build error
| import { Server } from "colyseus"; | |
| import { createServer } from "http"; | |
| import express from "express"; | |
| import cors from "cors"; | |
| // Import your room | |
| import { MyRoom } from "./rooms/MyRoom"; | |
| const port = Number(process.env.PORT || 7860); | |
| const app = express(); | |
| // Enable CORS for all origins (adjust for production) | |
| app.use(cors()); | |
| // Create HTTP server | |
| const server = createServer(app); | |
| // Create Colyseus server | |
| const gameServer = new Server({ | |
| server: server, | |
| }); | |
| // Define your room | |
| gameServer.define('my_room', MyRoom); | |
| // Optional: Serve static files (for a simple client) | |
| app.use(express.static('public')); | |
| // Health check endpoint | |
| app.get('/health', (req, res) => { | |
| const rooms = gameServer.matchMaker.getRooms(); | |
| const totalClients = rooms.reduce((sum, room) => sum + room.clients.length, 0); | |
| res.json({ | |
| status: 'ok', | |
| timestamp: new Date().toISOString(), | |
| activeRooms: rooms.length, | |
| totalConnections: totalClients, | |
| roomDetails: rooms.map(room => ({ | |
| roomId: room.roomId, | |
| name: room.roomName, | |
| clients: room.clients.length, | |
| maxClients: room.maxClients, | |
| locked: room.locked | |
| })) | |
| }); | |
| }); | |
| // Basic info endpoint | |
| app.get('/', (req, res) => { | |
| const rooms = gameServer.matchMaker.getRooms(); | |
| const totalClients = rooms.reduce((sum, room) => sum + room.clients.length, 0); | |
| res.json({ | |
| message: 'Top-down Game Server is running! ๐ฎ', | |
| endpoints: { | |
| health: '/health', | |
| monitor: '/colyseus', | |
| websocket: `ws://${req.get('host') || `localhost:${port}`}` | |
| }, | |
| stats: { | |
| activeRooms: rooms.length, | |
| connectedClients: totalClients, | |
| serverUptime: process.uptime() | |
| }, | |
| gameInfo: { | |
| roomType: 'my_room', | |
| maxPlayersPerRoom: 10, | |
| availableRooms: rooms.filter(room => !room.locked && room.clients.length < room.maxClients).length | |
| } | |
| }); | |
| }); | |
| // API endpoint to create/join a room (for testing) | |
| app.post('/join-room', async (req, res) => { | |
| try { | |
| const reservation = await gameServer.matchMaker.joinOrCreate('my_room', { | |
| name: req.body.playerName || 'Anonymous' | |
| }); | |
| res.json({ | |
| success: true, | |
| roomId: reservation.room.roomId, | |
| sessionId: reservation.sessionId, | |
| message: 'Room reservation created successfully' | |
| }); | |
| } catch (error) { | |
| res.status(500).json({ | |
| success: false, | |
| error: error instanceof Error ? error.message : 'Unknown error' | |
| }); | |
| } | |
| }); | |
| // Start the server | |
| gameServer.listen(port); | |
| console.log(`๐ฎ Game server listening on http://localhost:${port}`); | |
| console.log(`๐ Monitor panel: http://localhost:${port}/colyseus`); | |
| console.log(`๐ WebSocket endpoint: ws://localhost:${port}`); | |
| console.log(`๐ Room type available: my_room`); |