ColyseusJS 轻量级多人游戏服务器开发框架 - 中文手册(下)

本文档是ColyseusJS轻量级多人游戏服务器开发框架的中文手册下半部分,涵盖了状态处理、序列化方法、Schema、服务端和客户端的详细使用,包括原始类型、子Schema属性、ArraySchema、MapSchema、过滤每个客户端的数据等。同时介绍了如何在客户端和服务器端处理状态变更、回调函数以及最佳实践。
摘要由CSDN通过智能技术生成


快速上手多人游戏服务器开发。后续会基于 Google Agones,更新相关 K8S 运维、大规模快速扩展专用游戏服务器的文章。拥抱☁️原生🤗 Cloud-Native!

系列

状态处理

Colyseus 中,room handlers有状态(stateful) 的。每个房间都有自己的状态。状态的突变会自动同步到所有连接的客户端。

序列化方法

  • Schema (default)

状态同步时

  • user 成功加入 room 后,他将从服务器接收到完整状态。
  • 在每个 patchRate 处,状态的二进制补丁会发送到每个客户端(默认值为50ms)
  • 从服务器接收到每个补丁后,在客户端调用 onStateChange
  • 每种序列化方法都有自己处理传入状态补丁的特殊方式。

Schema

SchemaSerializer 是从 Colyseus 0.10 开始引入的,它是默认的序列化方法。

Schema 结构只用于房间的状态(可同步数据)。对于不能同步的算法中的数据,您需要使用 Schema 及其其他结构。

服务端

要使用 SchemaSerializer,你必须:

  • 有一个扩展 Schema 类的状态类
  • @type() 装饰器注释你所有的可同步属性
  • 为您的房间实例化状态(this.setState(new MyState()))
import { Schema, type } from "@colyseus/schema";

class MyState extends Schema {
    @type("string")
    currentTurn: string;
}

原始类型

这些是您可以为 @type() 装饰器提供的类型及其限制。

如果您确切地知道 number 属性的范围,您可以通过为其提供正确的原始类型来优化序列化。
否则,请使用 "number",它将在序列化过程中添加一个额外的字节来标识自己。

Type Description Limitation
"string" utf8 strings maximum byte size of 4294967295
"number" auto-detects the int or float type to be used. (adds an extra byte on output) 0 to 18446744073709551615
"boolean" true or false 0 or 1
"int8" signed 8-bit integer -128 to 127
"uint8" unsigned 8-bit integer 0 to 255
"int16" signed 16-bit integer -32768 to 32767
"uint16" unsigned 16-bit integer 0 to 65535
"int32" signed 32-bit integer -2147483648 to 2147483647
"uint32" unsigned 32-bit integer 0 to 4294967295
"int64" signed 64-bit integer -9223372036854775808 to 9223372036854775807
"uint64" unsigned 64-bit integer 0 to 18446744073709551615
"float32" single-precision floating-point number -3.40282347e+38 to 3.40282347e+38
"float64" double-precision floating-point number -1.7976931348623157e+308 to 1.7976931348623157e+308

子 schema 属性

您可以在 "root" 状态定义中定义更多自定义数据类型,如直接引用(direct reference)、映射(map)或数组(array)。

import { Schema, type } from "@colyseus/schema";

class World extends Schema {
    @type("number")
    width: number;

    @type("number")
    height: number;

    @type("number")
    items: number = 10;
}

class MyState extends Schema {
    @type(World)
    world: World = new World();
}

ArraySchema

ArraySchema 是内置 JavaScript Array 类型的可同步版本。

可以从数组中使用更多的方法。看看数组的 MDN 文档

示例:自定义 Schema 类型的数组

import { Schema, ArraySchema, type } from "@colyseus/schema";

class Block extends Schema {
    @type("number")
    x: number;

    @type("number")
    y: number;
}

class MyState extends Schema {
    @type([ Block ])
    blocks = new ArraySchema<Block>();
}

示例:基本类型的数组

您不能在数组内混合类型。

import { Schema, ArraySchema, type } from "@colyseus/schema";

class MyState extends Schema {
    @type([ "string" ])
    animals = new ArraySchema<string>();
}
array.push()

在数组的末尾添加一个或多个元素,并返回该数组的新长度。

const animals = new ArraySchema<string>();
animals.push("pigs", "goats");
animals.push("sheeps");
animals.push("cows");
// output: 4
array.pop()

从数组中删除最后一个元素并返回该元素。此方法更改数组的长度。

animals.pop();
// output: "cows"

animals.length
// output: 3
array.shift()

从数组中删除第一个元素并返回被删除的元素。这个方法改变数组的长度。

animals.shift();
// output: "pigs"

animals.length
// output: 2
array.unshift()

将一个或多个元素添加到数组的开头,并返回数组的新长度。

animals.unshift("pigeon");
// output: 3
array.indexOf()

返回给定元素在数组中的第一个下标,如果不存在则返回 -1

const itemIndex = animals.indexOf("sheeps");
array.spli
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值