latterworks commited on
Commit
872ab8c
·
verified ·
1 Parent(s): f455eaa

Update src/index.ts

Browse files
Files changed (1) hide show
  1. src/index.ts +40 -19
src/index.ts CHANGED
@@ -1,34 +1,55 @@
1
- // src/index.ts
2
- import http from "http";
3
  import express from "express";
4
  import cors from "cors";
5
- import { Server } from "@colyseus/core";
6
- import { WebSocketTransport } from "@colyseus/ws-transport";
7
  import { MyRoom } from "./rooms/MyRoom";
8
 
 
9
  const app = express();
 
 
10
  app.use(cors());
11
- app.use(express.json());
12
 
13
- const server = http.createServer(app);
 
14
 
 
15
  const gameServer = new Server({
16
- transport: new WebSocketTransport({ server }),
 
17
  });
18
 
19
- gameServer.define("my_room", MyRoom);
 
20
 
21
- const PORT = Number(process.env.PORT) || 2567; // Default to 2567 for local, 7860 on HF Spaces
 
22
 
23
- gameServer.listen(PORT)
24
- .then(() => {
25
- console.log(`Colyseus server listening on ws://localhost:${PORT}`);
26
- console.log("Room 'my_room' is ready for connections.");
27
- })
28
- .catch((err) => {
29
- console.error("Error starting Colyseus server:", err);
30
- });
31
 
32
- app.get("/", (req, res) => {
33
- res.send("Colyseus game server is running!");
 
 
 
 
 
 
 
 
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`);