可以在一个gatewayworker里同时设置两个businessworker?

veinliu

RT:
我这样设置了两个businessworker,两个一个$worker->eventHandler ='Events_SIS';一个默认,但是Gateway的数据怎么定指定到哪个businessworker,上来处理啊?
设置两个businessworker是可以运行,但是不知道怎么指定gateway的消息到其中的一个Gateway.

1906 5 0
5个回答

喵了个咪

gateway 可以设置消息路由,手册里有介绍

  • 暂无评论
blogdaren

方法就是楼上说的为gateway设置路由,主要是 $key 的定制, 以下是路由至特定businessworker参考代码:

$gateway->router = function($worker_connections, $client_connection, $cmd, $buffer){
    $target_businessworker = array(
        'ip'    => '127.0.0.1',          //填写目标 businessworker ip
        'name'  => 'demoBusiness2',      //填写目标 businessworker name
        'id'    => rand(0, 2),           //填写目标 businessworker id
    );  

    $key = implode(':', array_values($target_businessworker));
    empty($client_connection->businessworker_address) && $client_connection->businessworker_address = $key;

    return $worker_connections[$client_connection->businessworker_addres];
};
  • 暂无评论
veinliu

谢谢两位,看了好久的手册都没有,找到,谢谢!

  • 暂无评论
veinliu

Notice: Undefined index: 127.0.0.1:BusinessWorker_SIS:0 in /var/www/html/GatewayWorker/Applications/YourApp/start_sis_gateway.php on line 42
用了楼上的代码,不知道为什么报这个错误。

  • blogdaren 2019-11-23

    找不到 $key 呗, var_dump($worker_connections) 看下 $key 是否匹配。

veinliu
$gateway->router = function($worker_connections, $client_connection, $cmd, $buffer){
    $target_businessworker = array(
        'ip'    => '127.0.0.1',           //填写目标 businessworker ip
        'name'  => 'BusinessWorker_Exchange',       //填写目标 businessworker name
        'id'    => 0,  // rand(0, 1) ,            //填写目标 businessworker id
    );

    $key = implode(':', array_values($target_businessworker));
     empty($client_connection->businessworker_address) && $client_connection->businessworker_address = $key;

    // var_dump( $worker_connections);
    return $worker_connections[$client_connection->businessworker_address];
};

二楼的代码非常正确,只不过我的情况只有一个worker 所以ID 是0,然后我改成零不能用,后来var_dump发现没有错,经检查是因为0打了单引号。
初次写代码,不懂不好意思。

  • 暂无评论
年代过于久远,无法发表回答
🔝