请问下面这个定时器如何改成普通函数的形式?

小七他哥

代码如下,是workerStart下的定时器:

// 进程启动后设置一个每30秒运行一次的定时器,定时关闭不发送心跳包的链接以及没有sid属性的链接
Timer::add(30, 'cleanWorkerInvalidConnection', [$worker]);
function cleanWorkerInvalidConnection(&$worker) {
    $time_now = time();

    if (!$worker || empty($worker->connections)) {
        return;
    }

    foreach ($worker->connections as &$connection) {
        if (empty($connection->sid)) {
            $connection->close_reason = 'ip=' . $connection->getRemoteIp() . '的用户连接sid属性值为空';
            $connection->close("\r\n");
            continue;
        }
        // 有可能该connection还没收到过消息,则lastMessageTime设置为当前时间
        if (empty($connection->lastMessageTime)) {
            $connection->lastMessageTime = $time_now;
            continue;
        }
        // 上次通讯时间间隔大于心跳间隔,则认为客户端已经下线,关闭连接
        if ($time_now - $connection->lastMessageTime > HEARTBEAT_TIME) {
            if (isset($GLOBALS['worker_id_connection_id_sid_list'][getConnectionUniqueId($connection)])) {
                $connection->close_reason = 'sid=' . $GLOBALS['worker_id_connection_id_sid_list'][getConnectionUniqueId($connection)] . '的用户超时未发心跳包';
            } else {
                $connection->close_reason = 'sid=未知的用户超时未发心跳包';
            }

            $connection->close("\r\n");
        }
    }
}

现在服务器启动后,代码定时报错:

Warning: Parameter 1 to cleanWorkerInvalidConnection() expected to be a reference, value given in /home/www/diary/workerManApi/Workerman/Events/Select.php on line 232

请问这里该怎么改呢?

这里我如果改成非引用传参的形式,会不会起不到应有的作用呢?

1351 1 0
1个回答

phpcreeper

function cleanWorkerInvalidConnection(&$worker)
改成
function cleanWorkerInvalidConnection($worker)

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