官方包 · 游戏引擎

用 WebSocket 打造 Phaser 多人房间

完整的 Phaser 4 示例:房间列表、四人座位、房主、准备状态、带输入中提示的聊天和反应力游戏,全部绘制在 canvas 上。Phaser、浏览器和 Godot 玩家可以在同一个房间里玩。

  • Lobby 包只依赖 pusher-js,在 Phaser 中可以直接使用。
  • 每次点击都是房间频道上的客户端事件,无需部署游戏服务器。
  • 名称和聊天用的是叠在 canvas 上的真实输入框,中文、日文和韩文输入法都能用。

登录后,示例代码会自动填入您的集群主机。 登录

  1. 安装

    git clone https://github.com/hibira-io/examples
    cd examples/phaser-lobby
    npm install && npm run dev
  2. 使用

    import Pusher from 'pusher-js';
    import { Room } from '@hibira-io/lobby';
    
    const pusher = new Pusher(import.meta.env.VITE_HIBIRA_APP_KEY, {
      cluster: 'hkg',
      wsHost: 'ws.hibira.io',
      channelAuthorization: { endpoint: '/api/hibira/auth' },
    });
    const room = Room.join(pusher, 'presence-room-42', { capacity: 4 });
    room.on('start', ({ data }) => this.scene.start('Game', data));

开始之前

  • 在应用设置中开启“允许客户端事件”,玩家之间才能互相发送操作和点击。
  • 私有频道和 presence 频道需要后端提供授权端点,用应用密钥签名订阅,做法与 pusher-js 相同。

请使用控制台中应用的 Key;Secret 只放在你的服务器上。频道、事件与授权端点的用法与 Pusher 完全相同。 免费创建账号 →