Skip to content
官方QQ交流群
技术交流1:87208295   点此加入
技术交流2:787747122   点此加入
官网
云控制台
开放平台
关注微信公众号
代码仓库: 码云

gateway进程

简要

  • 物联网、IM、游戏等
  • 支持分布式

注解支持

  • 支持 GatewayBusinessWorker
  • 支持 GatewayRegister
  • 支持 GatewayGateway

配置

/config/shiyun/worker_gateway.php 中配置

php
<?php  
return [
    // 使用注解注册,存在server的路径,支持*
    'annotation_path' => [
        'addons/moduleA/worker',
        'addons/*/worker',
        'addons/*/*/worker', 
    ]
];
<?php  
return [
    // 使用注解注册,存在server的路径,支持*
    'annotation_path' => [
        'addons/moduleA/worker',
        'addons/*/worker',
        'addons/*/*/worker', 
    ]
];

示例

\addons\moduleA\worker\TestEvents.php

php
<?php

namespace addons\moduleA\worker;

use shiyunWorker\annotation\GatewayBusinessWorker;
use shiyunWorker\annotation\GatewayGateway;
use shiyunWorker\annotation\GatewayRegister;
use \GatewayWorker\Lib\Gateway;

/**
 * 主逻辑
 * 主要是处理 onConnect onMessage onClose 三个方法
 * onConnect 和 onClose 如果不需要可以不用实现并删除
 */
#[GatewayBusinessWorker(registerAddress: '127.0.0.1:1238')]
#[GatewayRegister(socket: 'text://0.0.0.0:1238')]
// gateway 进程
#[GatewayGateway(
    registerAddress: '127.0.0.1:1238',
    socket: 'tcp://0.0.0.0:8282',
    startPort: 2900,
)]
class TestEvents
{
    /**
     * 当客户端连接时触发
     * 如果业务不需此回调可以删除onConnect
     * 
     * @param int $client_id 连接id
     */
    public static function onConnect($client_id)
    {
        // 向当前client_id发送数据 
        Gateway::sendToClient($client_id, "Hello $client_id\r\n");
        // 向所有人发送
        Gateway::sendToAll("$client_id login\r\n");
    }

    /**
     * 当客户端发来消息时触发
     * @param int $client_id 连接id
     * @param mixed $message 具体消息
     */
    public static function onMessage($client_id, $message)
    {
        // 向所有人发送 
        Gateway::sendToAll("$client_id said $message\r\n");
    }

    /**
     * 当用户断开连接时触发
     * @param int $client_id 连接id
     */
    public static function onClose($client_id)
    {
        // 向所有人发送 
        GateWay::sendToAll("$client_id logout\r\n");
    }
}
<?php

namespace addons\moduleA\worker;

use shiyunWorker\annotation\GatewayBusinessWorker;
use shiyunWorker\annotation\GatewayGateway;
use shiyunWorker\annotation\GatewayRegister;
use \GatewayWorker\Lib\Gateway;

/**
 * 主逻辑
 * 主要是处理 onConnect onMessage onClose 三个方法
 * onConnect 和 onClose 如果不需要可以不用实现并删除
 */
#[GatewayBusinessWorker(registerAddress: '127.0.0.1:1238')]
#[GatewayRegister(socket: 'text://0.0.0.0:1238')]
// gateway 进程
#[GatewayGateway(
    registerAddress: '127.0.0.1:1238',
    socket: 'tcp://0.0.0.0:8282',
    startPort: 2900,
)]
class TestEvents
{
    /**
     * 当客户端连接时触发
     * 如果业务不需此回调可以删除onConnect
     * 
     * @param int $client_id 连接id
     */
    public static function onConnect($client_id)
    {
        // 向当前client_id发送数据 
        Gateway::sendToClient($client_id, "Hello $client_id\r\n");
        // 向所有人发送
        Gateway::sendToAll("$client_id login\r\n");
    }

    /**
     * 当客户端发来消息时触发
     * @param int $client_id 连接id
     * @param mixed $message 具体消息
     */
    public static function onMessage($client_id, $message)
    {
        // 向所有人发送 
        Gateway::sendToAll("$client_id said $message\r\n");
    }

    /**
     * 当用户断开连接时触发
     * @param int $client_id 连接id
     */
    public static function onClose($client_id)
    {
        // 向所有人发送 
        GateWay::sendToAll("$client_id logout\r\n");
    }
}

指令

sh
php think worker:gateway 
php think worker:gateway status
php think worker:gateway start
php think worker:gateway -d
php think worker:gateway start -d
php think worker:gateway stop
php think worker:gateway reload
php think worker:gateway 
php think worker:gateway status
php think worker:gateway start
php think worker:gateway -d
php think worker:gateway start -d
php think worker:gateway stop
php think worker:gateway reload

端口开放

开启端口

开启

进入容器

php think worker
php think worker

调试

sh

# 连接调试
telnet 127.0.0.1 2347


# 退出
ctrl+]  在输入 quit

# 连接调试
telnet 127.0.0.1 2347


# 退出
ctrl+]  在输入 quit

Copyright © 2017 10yun.com | 十云提供计算服务-IPV6 | ctocode组开发