Skip to content

从 0.12 版本迁移至 0.13 版本

0.13 版本引入了以下重大变化:

请安装最新版本您的服务器和客户端:

升级服务器:

npm install colyseus@^0.14.0 --save

升级客户端:

npm install colyseus.js@^0.14.0 --save

若您使用的是 Unity 或其他客户端,请在 GitHub 上查看相应资源库.如果您有任何问题, 欢迎在 Discord 上进行询问

onMessage() 被重做

在所有之前版本的 Colyseus 中,您需要通过自己的特定规则手动检测正在发送的信息类型.

服务器端

// 0.12 (old)

class MyRoom extends Room {
  onMessage(client, message) {
    //
    // handle message
    //
  }
}

借助 重做后的 onMessage(), 您可以通过客户端发送的消息类型来触发特定的回调.

第一个参数可以是一个字符串,也可以是一个数字. (点此查看完整的 onMessage 文档)

// 0.13 (new)

class MyRoom extends Room {
  onCreate(options) {
    this.onMessage("message_type_1", (client, message) => {
      //
      // handle 'message_type_1' here
      //
    });

    this.onMessage("message_type_2", (client, message) => {
      //
      // handle 'message_type_2' here
      //
    });

  }
}

this.broadcast(message) 现改为 this.broadcast(type, message)

由于每条消息现在都需要有一个type, 因此 this.broadcast() 方式也做了修改:

// 0.12 (old)
this.broadcast({ foo: { bar: "baz" } });

// 0.13 (new)
this.broadcast("foo", { bar: "baz" });

this.send(client, message) 现改为 client.send(type, message)

this.send() 方法已被弃用.每条消息现在需要一个 type, 您现在可以使用 client.send(type, message):

// 0.12 (old)
this.send(client, { foo: { bar: "baz" } });

// 0.13 (new)
client.send("foo", { bar: "baz" });

客户端

// 0.12 (old)
room.send({ type: "xxx", anything: "your data" });

// 0.13 (new)
room.send("xxx", { anything: "your data" });
// 0.12 (old)
room.Send(new { type = "xxx", anything = "your data" });

// 0.13 (new)
room.Send("xxx", new { anything = "your data" });
-- 0.12.x
room:send({ type = "xxx", anything = "your data" })

-- 0.13.x
room:send("xxx", { anything = "your data" })
// 0.12 (old)
room.send({ type: "xxx", anything: "your data" });

// 0.13 (new)
room.send("xxx", { anything: "your data" });
// 0.12 (old)
room.send({{"type", "xxx"}, {"anything", "your data"}});

// 0.13 (new)
room.send("xxx", {{"anything", "your data"}});

onError() 及匹配错误

匹配错误现包含错误代码.

匹配错误现在包含错误代码.

// 0.12 (old)
try {
  const room = await client.joinOrCreate("battle");

} catch (e) {
  console.log("Error message:", e);
}
// 0.13 (new)
try {
  const room = await client.joinOrCreate("battle");

} catch (e) {
  console.log("Error code:", e.code);
  console.log("Error message:", e.message);
}

onError() 现在有了代码消息.

客户端的 onError() 回调现在有可用的 代码消息 了,不像以前只有 消息.

// 0.12 (old)
room.onError((message) => {
  console.log("oops, error ocurred:");
  console.log(message);
});

// 0.13 (new)
room.onError((code, message) => {
  console.log("oops, error ocurred:");
  console.log(message);
});
// 0.12 (old)
room.OnError += (message) => {
  Debug.Log ("oops, error ocurred:");
  Debug.Log(message);
}

// 0.13 (new)
room.OnError += (code, message) => {
  Debug.Log ("oops, error ocurred:");
  Debug.Log(message);
}
-- 0.12.x
room:on("error", function(message)
  print("oops, error ocurred:")
  print(message)
end)

-- 0.13.x
room:on("error", function(code, message)
  print("oops, error ocurred:")
  print(message)
end)
// 0.12 (old)
room.onError += function(message) {
  trace("oops, error ocurred:");
  trace(message);
};

// 0.13 (new)
room.onError += function(code, message) {
  trace("oops, error ocurred:");
  trace(message);
};
// 0.12 (old)
room.onError = [=] (std::string message) => void {
  std::cout << "oops, error ocurred: " << message << std::endl;
};

// 0.13 (new)
room.onError = [=] (int code, std::string message) => void {
  std::cout << "oops, error ocurred: " << message << std::endl;
};
Back to top