开启异步AsyncTcpConnection,异步处理数据,防止主干道阻塞?

lichao
<?php
use \GatewayWorker\Lib\Gateway;
use \Workerman\Worker;
use \Workerman\Connection\AsyncTcpConnection;
use \Applications\YourApp\Controller\MyWm;
use \Applications\YourApp\Database\MySql; 
require_once __DIR__.'/../Function/functions.php';  
class Admin extends MyWm{
    public function verify($args,$client_id)
    {
        $id             =       $args;  
        $onlinetime     =       $args;
        $db             =       new MySql();
        $sql            =       "select title,publishtime,content,price,
        img from tbl_order_content where id=$id";
        $info           =       $db->get_one($sql);

             **这里不用一步的话会阻塞主干道,怎么使用异步AsyncTcpConnection,在异步里操作数据库**
    }
}
?>

上面是我的控制器

手册里的解析:

use \Workerman\Worker;
use \Workerman\Connection\AsyncTcpConnection;
require_once './Workerman/Autoloader.php';

$task = new Worker();
// 进程启动时异步建立一个到www.baidu.com连接对象,并发送数据获取数据
$task->onWorkerStart = function($task)
{
    $connection_to_baidu = new AsyncTcpConnection('tcp://www.baidu.com:80');
    // 当连接建立成功时,发送http请求数据
    $connection_to_baidu->onConnect = function($connection_to_baidu)
    {
        echo "connect success\n";
        $connection_to_baidu->send("GET / HTTP/1.1\r\nHost: www.baidu.com\r\nConnection: keep-alive\r\n\r\n");
    };
    $connection_to_baidu->onMessage = function($connection_to_baidu, $http_buffer)
    {
        echo $http_buffer;
    };
    $connection_to_baidu->onClose = function($connection_to_baidu)
    {
        echo "connection closed\n";
    };
    $connection_to_baidu->onError = function($connection_to_baidu, $code, $msg)
    {
        echo "Error code:$code msg:$msg\n";
    };
    $connection_to_baidu->connect();
};

// 运行worker
Worker::runAll();

问题:怎么整合起来,求大大给我一个案例我参考一下就好了。或者简单的即行伪代码也可以。

5081 1 0
1个回答

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