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