public function main()
{
$worker = new Worker();
$worker->onWorkerStart = function () {
Timer::add(1, function () {
//這里的代碼,并不能在一秒內(nèi)執(zhí)行完成
for ($i = 0; $i <= 1000; $i++) {
$this->wsClient();
}
});
Timer::add(1, function () {
dump("qps :{$this->qps}");
$count = count($this->conns);
dump("連接數(shù):$count");
$this->qps = 0;
});
};
$this->worker = new Worker('websocket://127.0.0.1:7272');
$this->worker->onConnect = function (ConnectionInterface $conn) {
$id = spl_object_hash($conn);
$this->conns[$id] = $conn;
};
$this->worker->onConnect = function (ConnectionInterface $conn) {
$id = spl_object_hash($conn);
$this->conns[$id] = $conn;
};
$this->worker->onClose = function (ConnectionInterface $conn) {
$id = spl_object_hash($conn);
unset($this->conns[$id]);
};
Worker::runAll();
}
private function wsClient()
{
$ws = new AsyncTcpConnection('ws://127.0.0.1:7272');
$ws->onConnect = function (ConnectionInterface $conn) {
Timer::add(1, function () use ($conn) {
$conn->send('hello world');
$this->qps++;
});
};
$ws->onError = function ($conn, $code, $msg) {
dump($msg);
};
$ws->connect();
}
機器是m1 MacBook air 16G
無論是用event還是select都是差不多的每秒連接數(shù)