Spaces:
Build error
Build error
Update src/index.ts
Browse files- src/index.ts +40 -19
src/index.ts
CHANGED
|
@@ -1,34 +1,55 @@
|
|
| 1 |
-
|
| 2 |
-
import
|
| 3 |
import express from "express";
|
| 4 |
import cors from "cors";
|
| 5 |
-
|
| 6 |
-
|
| 7 |
import { MyRoom } from "./rooms/MyRoom";
|
| 8 |
|
|
|
|
| 9 |
const app = express();
|
|
|
|
|
|
|
| 10 |
app.use(cors());
|
| 11 |
-
app.use(express.json());
|
| 12 |
|
| 13 |
-
|
|
|
|
| 14 |
|
|
|
|
| 15 |
const gameServer = new Server({
|
| 16 |
-
|
|
|
|
| 17 |
});
|
| 18 |
|
| 19 |
-
|
|
|
|
| 20 |
|
| 21 |
-
|
|
|
|
| 22 |
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
.
|
| 29 |
-
|
| 30 |
-
|
| 31 |
|
| 32 |
-
|
| 33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { Server } from "colyseus";
|
| 2 |
+
import { createServer } from "http";
|
| 3 |
import express from "express";
|
| 4 |
import cors from "cors";
|
| 5 |
+
|
| 6 |
+
// Import your room
|
| 7 |
import { MyRoom } from "./rooms/MyRoom";
|
| 8 |
|
| 9 |
+
const port = Number(process.env.PORT || 7860);
|
| 10 |
const app = express();
|
| 11 |
+
|
| 12 |
+
// Enable CORS for all origins (adjust for production)
|
| 13 |
app.use(cors());
|
|
|
|
| 14 |
|
| 15 |
+
// Create HTTP server
|
| 16 |
+
const server = createServer(app);
|
| 17 |
|
| 18 |
+
// Create Colyseus server
|
| 19 |
const gameServer = new Server({
|
| 20 |
+
server: server,
|
| 21 |
+
express: app,
|
| 22 |
});
|
| 23 |
|
| 24 |
+
// Define your room
|
| 25 |
+
gameServer.define('my_room', MyRoom);
|
| 26 |
|
| 27 |
+
// Optional: Serve static files (for a simple client)
|
| 28 |
+
app.use(express.static('public'));
|
| 29 |
|
| 30 |
+
// Health check endpoint
|
| 31 |
+
app.get('/health', (req, res) => {
|
| 32 |
+
res.json({
|
| 33 |
+
status: 'ok',
|
| 34 |
+
timestamp: new Date().toISOString(),
|
| 35 |
+
rooms: gameServer.presence.keys()
|
| 36 |
+
});
|
| 37 |
+
});
|
| 38 |
|
| 39 |
+
// Basic info endpoint
|
| 40 |
+
app.get('/', (req, res) => {
|
| 41 |
+
res.json({
|
| 42 |
+
message: 'Top-down Game Server is running!',
|
| 43 |
+
endpoints: {
|
| 44 |
+
health: '/health',
|
| 45 |
+
monitor: '/colyseus',
|
| 46 |
+
websocket: `ws://localhost:${port}`
|
| 47 |
+
}
|
| 48 |
+
});
|
| 49 |
});
|
| 50 |
+
|
| 51 |
+
// Start the server
|
| 52 |
+
gameServer.listen(port);
|
| 53 |
+
|
| 54 |
+
console.log(`🎮 Game server listening on http://localhost:${port}`);
|
| 55 |
+
console.log(`📊 Monitor panel: http://localhost:${port}/colyseus`);
|