官方套件 · 遊戲引擎

Cocos Creator 的多人房間

無相依套件的 Cocos Creator 3.x 用戶端,支援網頁、原生與小遊戲建置;HibiraConnection 元件可跨場景保持連線,房間提供座位、房主、準備狀態與聊天。

  • 房間使用與 Lobby 套件相同的事件,Cocos 玩家和瀏覽器玩家可以同房。
  • 內含 Lobby 套件的配對佇列:玩家排隊、依評分與條件配對,然後一起進入新房間。
  • 會自動重新連線,並重新訂閱原本的頻道。
  • 授權端點是可在免費方案執行的 Cloudflare Worker。

登入後,範例程式碼會自動帶入您的叢集主機。 登入

  1. 設定

    git clone https://github.com/hibira-io/cocos && cd cocos/server
    # put your app key in wrangler.toml, then:
    npx wrangler deploy
    npx wrangler secret put HIBIRA_APP_SECRET
  2. 安裝

    Copy assets/hibira into your project's assets/.
    Add the HibiraConnection component to a root node of the first scene:
      App Key:        YOUR_APP_KEY
      Host:           ws.hibira.io
      Auth Endpoint:  your Worker's URL
  3. 使用

    import { HibiraConnection } from './hibira/HibiraConnection';
    
    const hibira = HibiraConnection.instance!;
    hibira.setName('Ann');
    const room = hibira.joinRoom('ABCD', { capacity: 4 });
    room.on('members', ({ players, host }) => renderSeats(players, host));
    room.on('start', ({ data }) => startGame(data.seed));

開始之前

  • 在應用程式設定中開啟「允許用戶端事件」,玩家之間才能互傳動作與點擊。
  • 把 http://localhost:7456(編輯器的瀏覽器預覽)和每個網頁建置的網站加入應用程式的「允許的來源」。小遊戲建置還要把 WebSocket 主機和授權端點加入平台的合法網域。

請使用儀表板中的應用程式 Key;Secret 只放在你的伺服器上。頻道、事件與授權端點的用法和 Pusher 完全相同。 免費建立帳號 →