onBusinessWorkerConnected 这个属性是不是没有用呢?

sunwenzheng

文件:vendor\workerman\gateway-worker\src\Gateway.php


    /**
     * 当 worker 发来数据时
     *
     * @param TcpConnection $connection
     * @param mixed         $data
     * @throws \Exception
     *
     * @return void
     */
    public function onWorkerMessage($connection, $data)
    {
        $cmd = $data['cmd'];
        if (empty($connection->authorized) && $cmd !== GatewayProtocol::CMD_WORKER_CONNECT && $cmd !== GatewayProtocol::CMD_GATEWAY_CLIENT_CONNECT) {
            self::log("Unauthorized request from " . $connection->getRemoteIp() . ":" . $connection->getRemotePort());
            $connection->close();
            return;
        }
        switch ($cmd) {
            // BusinessWorker连接Gateway
            case GatewayProtocol::CMD_WORKER_CONNECT:
                $worker_info = json_decode($data['body'], true);
                if ($worker_info['secret_key'] !== $this->secretKey) {
                    self::log("Gateway: Worker key does not match ".var_export($this->secretKey, true)." !== ". var_export($this->secretKey));
                    $connection->close();
                    return;
                }
                $key = $connection->getRemoteIp() . ':' . $worker_info['worker_key'];
                // 在一台服务器上businessWorker->name不能相同
                if (isset($this->_workerConnections[$key])) {
                    self::log("Gateway: Worker->name conflict. Key:{$key}");
                    $connection->close();
                    return;
                }
                $connection->key = $key;
                $this->_workerConnections[$key] = $connection;
                $connection->authorized = true;
                // onBusinessWorkerConnected 这里没找到在哪里赋值的
                if ($this->onBusinessWorkerConnected) {
                    call_user_func($this->onBusinessWorkerConnected, $connection);
                }
                return;

我搜了代码 找 onBusinessWorkerConnected 在哪里赋值的,结果没找到,只有初始化时的一个null

    /**
     * BusinessWorker 连接成功之后触发
     *
     * @var callback|null
     */
    public $onBusinessWorkerConnected = null;

我怕自己代码不是最新的,又搜了下代码库
https://github.com/walkor/GatewayWorker/search?q=onBusinessWorkerConnected
也没有找到,

请问 onBusinessWorkerConnected 这个属性是不是没有用呢?

742 1 0
1个回答

phpcreeper

有用,其明显是在应用层面预留给开发者的一个回调属性,比如在start_gateway.php里按需调用:

$gateway->onBusinessWorkerConnected = function($connection){
    //do sth
};
  • sunwenzheng 2022-08-09

    你说的对,这个是留给开发者自己写回调逻辑的,我一开始以为是gateway-worker自己内部做了处理记录,

年代过于久远,无法发表回答
×
🔝