PHP SDK for reactphp-x/gateway-worker,API 与 Workerman GatewayClient 兼容。
用于在 Web 请求、CLI 脚本 等 BusinessWorker 进程之外,向 GatewayWorker 集群中的客户端推送消息。
composer require reactphp-x/gateway-client本地开发(path 仓库):
{
"repositories": [
{ "type": "path", "url": "../gateway-client" }
],
"require": {
"reactphp-x/gateway-client": "@dev"
}
}use ReactX\GatewayClient\Gateway;
Gateway::$registerAddress = '127.0.0.1:1238'; // Register 服务地址
Gateway::$secretKey = ''; // 与 Gateway/BusinessWorker 一致registerAddress 指向 Register 进程监听的地址(与 start_register.php 一致)。GatewayClient 通过 Register 发现所有 Gateway 内网通讯地址,再直连 Gateway 推送数据。
<?php
require __DIR__ . '/vendor/autoload.php';
use ReactX\GatewayClient\Gateway;
Gateway::$registerAddress = '127.0.0.1:1238';
// 广播
Gateway::sendToAll("Hello everyone\n");
// 单发
Gateway::sendToClient($clientId, "Hello\n");
// 查询
Gateway::isOnline($clientId);
Gateway::getAllClientCount();
Gateway::getAllClientSessions();
// UID / 分组
Gateway::bindUid($clientId, $uid);
Gateway::sendToUid($uid, $message);
Gateway::joinGroup($clientId, 'room1');
Gateway::sendToGroup('room1', $message);
// Session
Gateway::setSession($clientId, ['name' => 'Tom']);
Gateway::getSession($clientId);- 启动 GatewayWorker:
cd gateway-worker/examples && php start.php start - 浏览器打开 WebSocket 测试页连接
- 运行推送示例:
cd gateway-client/examples
php push.php "Hello from GatewayClient"支持 Workerman GatewayClient 的主要方法(sendToCurrentClient / closeCurrentClient 仅 BusinessWorker 内可用,本 SDK 不提供)。
MIT