Spaces:
Build error
Build error
File size: 2,762 Bytes
872ab8c 7c76abf 872ab8c 7c76abf 872ab8c 7c76abf 872ab8c 7c76abf 872ab8c 7c76abf 872ab8c 7c76abf 872ab8c 7c76abf 872ab8c 7c76abf 872ab8c 7c76abf 872ab8c abaa493 872ab8c abaa493 872ab8c 7c76abf 872ab8c abaa493 872ab8c abaa493 872ab8c abaa493 47f4207 abaa493 872ab8c 7c76abf 872ab8c abaa493 872ab8c abaa493 |
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 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
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`); |