文件:vendor\workerman\gateway-worker\src\Gateway.php
/**
* 當(dāng) worker 發(fā)來(lái)數(shù)據(jù)時(shí)
*
* @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'];
// 在一臺(tái)服務(wù)器上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 這里沒(méi)找到在哪里賦值的
if ($this->onBusinessWorkerConnected) {
call_user_func($this->onBusinessWorkerConnected, $connection);
}
return;
我搜了代碼 找 onBusinessWorkerConnected 在哪里賦值的,結(jié)果沒(méi)找到,只有初始化時(shí)的一個(gè)null
/**
* BusinessWorker 連接成功之后觸發(fā)
*
* @var callback|null
*/
public $onBusinessWorkerConnected = null;
我怕自己代碼不是最新的,又搜了下代碼庫(kù)
https://github.com/walkor/GatewayWorker/search?q=onBusinessWorkerConnected
也沒(méi)有找到,
請(qǐng)問(wèn) onBusinessWorkerConnected 這個(gè)屬性是不是沒(méi)有用呢?
有用,其明顯是在應(yīng)用層面預(yù)留給開發(fā)者的一個(gè)回調(diào)屬性,比如在start_gateway.php里按需調(diào)用:
$gateway->onBusinessWorkerConnected = function($connection){
//do sth
};
你說(shuō)的對(duì),這個(gè)是留給開發(fā)者自己寫回調(diào)邏輯的,我一開始以為是gateway-worker自己內(nèi)部做了處理記錄,