官方包 · 游戏引擎

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 完全相同。 免费创建账号 →