Skip to content

JavaScript/TypeScript SDK

JavaScript/TypeScript SDK 幾乎與所有平臺兼容:

用法

將 JavaScript SDK 引入您的項目

如果您在使用構建工具(webpack, rollup 或類似工具), 這是最佳方式

npm install --save colyseus.js

若您不使用構建工具, 推薦從 GitHub Releases 下載發布的二進製文件.

<script src="colyseus.js"></script>

或者您也可以直接使用 unpkg 加載分布式文件. 記得將其中的 @x.x.x 替換為與您的服務器兼容的版本.

<script src="https://unpkg.com/colyseus.js@^0.14.0/dist/colyseus.js"></script>

連接至服務器:

import * as Colyseus from "colyseus.js"; // 用 <script> 載入的話不用寫這句.

var client = new Colyseus.Client('ws://localhost:2567');

加入房間:

client.joinOrCreate("room_name").then(room => {
    console.log(room.sessionId, "joined", room.name);
}).catch(e => {
    console.log("JOIN ERROR", e);
});

房間事件

房間 state 更新:

room.onStateChange((state) => {
  console.log(room.name, "has new state:", state);
});

從服務器廣播或直接發給該客戶端的消息:

room.onMessage("message_type", (message) => {
  console.log(client.id, "received on", room.name, message);
});

發生服務器錯誤:

room.onError((code, message) => {
  console.log(client.id, "couldn't join", room.name);
});

客戶端離開房間:

room.onLeave((code) => {
  console.log(client.id, "left", room.name);
});

Back to top