swoole实现一个定时异步http处理请求回调服务器代码

代码语言:phpcli

所属分类:通讯

代码描述:swoole实现一个定时异步http处理请求回调服务器代码

代码标签: swoole 定时 异步 http 回调

下面为部分代码预览,完整代码请点击下载或在bfwstudio webide中打开

<?php
$timerlist = [];
//定时任务列表缓存到本地,这样重启后可以继续执行
$_dishfile = __DIR__."/timerdisk";
if (file_exists($_dishfile)) {
    $timerlist = unserialize(file_get_contents($_dishfile));
}

//执行定时任务
function task($_intv, $_callbackurl) {

    Swoole\Timer::after($_intv*1000, function() use ($_intv, $_callbackurl) {

        $_data = file_get_contents($_callbackurl);
        global  $timerlist;
        $key = array_search($_callbackurl, array_keys($timerlist));
        if ($key !== false) {
            array_splice($timerlist, $key, 1);
            savedisk();
        }

        file_put_contents(__DIR__."/log.txt", "get ret {$_callbackurl} data:{$_data}");

        $_data = null;
        $_intv = null;
        $_callbackurl = null;

    });
}
//保存到本地
function savedisk() {
    global  $timerlist;
    $_dishfile = __DIR__."/timerdisk";

    file_put_contents($_dishfile,
        serialize($timerlist));
}


//高性能HTTP服务器
$http = new Swoole\Http\Server("0.0.0.0", 9502);

$http->on("start", function ($server) {
    echo "Swoole http server is started at port9501\n";
    global  $timerlist;
    foreach ($timerlist as $key => $val) {
        $_exetime = intval($val);
        $_callbackurl = $key;
        if ($_exetime > time()) {
            $_intv = $_exetime-time();
            echo &q.........完整代码请登录后点击上方下载按钮下载查看

网友评论0