colyseus server side example usage
Rate quality
Private research annotation
Sign in to add a private research label or note.
- User
colyseus server side example usage
- Assistant
Sign in to add a private research label or note.
colyseus server side example usage
Colyseus is a multiplayer game server that provides a simple way to manage state synchronization between clients and the server. Below is a basic example to help you set up a Colyseus server with TypeScript. This example demonstrates creating a game room where players can join, and the server keeps track of their positions.
First, create a new directory and initialize it with npm:
mkdir colyseus-example
cd colyseus-example
npm init -y
Install Colyseus and TypeScript with the following command:
npm install colyseus@latest @colyseus/schema@latest
npm install --save-dev typescript ts-node @types/node
Create tsconfig.json to configure TypeScript:
{
"compilerOptions": {
"target": "ES2019",
"module": "CommonJS",
"outDir": "dist",
"strict": true,
"esModuleInterop": true
},
"include": ["src/**/*.ts"],
"exclude": ["node_modules"]
}
Create a directory for your source files and a new file for your room:
mkdir src
touch src/GameRoom.ts
Now, open src/GameRoom.ts and implement a simple game room that allows players to join and track their position. Here's an example implementation:
import { Room, Client } from "colyseus";
import { Schema, model, type } from "@colyseus/schema";
// Define the Player schema
class Player extends Schema {
@type("string")
id: string;
@type("number")
x: number;
@type("number")
y: number;
constructor(id: string, x: number = 0, y: number = 0) {
super();
this.id = id;
this.x = x;
this.y = y;
}
}
// Define the GameRoom schema
class GameRoomState extends Schema {
@type({ map: Player })
players = new Map<string, Player>();
}
export class GameRoom extends Room<GameRoomState> {
onCreate(options: any) {
this.setState(new GameRoomState());
this.onMessage("move", (client, message) => {
const player = this.state.players.get(client.sessionId);
if (player) {
player.x += message.dx;
player.y += message.dy;
this.broadcast("players", this.state.players);
}
});
this.onMessage("disconnect", (client) => {
this.state.players.delete(client.sessionId);
});
}
onJoin(client: Client, options: any) {
const player = new Player(client.sessionId);
this.state.players.set(client.sessionId, player);
this.broadcast("players", this.state.players);
}
onLeave(client: Client, consented: boolean) {
this.state.players.delete(client.sessionId);
this.broadcast("players", this.state.players);
}
}
Create a new file src/index.ts to set up your Colyseus server:
import { Server } from "colyseus";
import { createServer } from "http";
import { GameRoom } from "./GameRoom";
const httpServer = createServer();
const gameServer = new Server({ server: httpServer });
gameServer.define("game_room", GameRoom);
httpServer.listen(2567, () => {
console.log("Listening on http://localhost:2567");
});
package.json to easily run the server in TypeScript:"scripts": {
"start": "ts-node src/index.ts"
}
npm start
You now have a basic Colyseus server running. The server can handle players joining and leaving, as well as simple movement commands sent from clients.
For actual client implementation, you would leverage Colyseus client-side library to connect, send messages, and receive updates. Make sure to adjust your GameRoom logic and message handling as per your game's requirements.
For a full production setup, consider incorporating logging, error handling, and more structured game logic. Additionally, refer to the Colyseus documentation for more advanced features and best practices.
Not yet included in a published release.