Skip to content

reactphp-x/gateway-client

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

reactphp-x/gateway-client

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 示例联调

  1. 启动 GatewayWorker:cd gateway-worker/examples && php start.php start
  2. 浏览器打开 WebSocket 测试页连接
  3. 运行推送示例:
cd gateway-client/examples
php push.php "Hello from GatewayClient"

API 说明

支持 Workerman GatewayClient 的主要方法(sendToCurrentClient / closeCurrentClient 仅 BusinessWorker 内可用,本 SDK 不提供)。

License

MIT

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages