Update server.js
Browse files
server.js
CHANGED
|
@@ -1,19 +1,48 @@
|
|
| 1 |
-
const
|
|
|
|
|
|
|
| 2 |
|
| 3 |
-
|
|
|
|
| 4 |
|
| 5 |
-
|
| 6 |
-
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
const colyseus = require("colyseus");
|
| 2 |
+
const http = require("http");
|
| 3 |
+
const express = require("express"); // optional
|
| 4 |
|
| 5 |
+
// Initialize the Express app (optional)
|
| 6 |
+
const app = express();
|
| 7 |
|
| 8 |
+
// Create HTTP & WebSocket servers
|
| 9 |
+
const server = http.createServer(app);
|
| 10 |
+
const gameServer = new colyseus.Server({
|
| 11 |
+
server: server,
|
| 12 |
+
});
|
| 13 |
|
| 14 |
+
// Define a room handler
|
| 15 |
+
class MyRoom extends colyseus.Room {
|
| 16 |
+
onCreate(options) {
|
| 17 |
+
console.log("Room created!", options);
|
| 18 |
+
}
|
| 19 |
|
| 20 |
+
onJoin(client, options) {
|
| 21 |
+
console.log(client.sessionId, "joined!");
|
| 22 |
+
}
|
| 23 |
+
|
| 24 |
+
onLeave(client, consented) {
|
| 25 |
+
console.log(client.sessionId, "left!");
|
| 26 |
+
}
|
| 27 |
+
|
| 28 |
+
onMessage(client, message) {
|
| 29 |
+
console.log(client.sessionId, "sent message", message);
|
| 30 |
+
this.broadcast("messages", message);
|
| 31 |
+
}
|
| 32 |
|
| 33 |
+
onDispose() {
|
| 34 |
+
console.log("Room disposed!");
|
| 35 |
+
}
|
| 36 |
+
}
|
| 37 |
+
|
| 38 |
+
// Register the room handler
|
| 39 |
+
gameServer.define("my_room", MyRoom);
|
| 40 |
+
|
| 41 |
+
// Serve static files (optional)
|
| 42 |
+
app.use(express.static("public"));
|
| 43 |
+
|
| 44 |
+
// Start the server
|
| 45 |
+
const port = process.env.PORT || 7860;
|
| 46 |
+
server.listen(port, () => {
|
| 47 |
+
console.log(`Listening on ws://localhost:${port}`);
|
| 48 |
+
});
|