swoole实例

2020-12-24 15:29:39 阅读:2 编辑

使用Etag

https://www.cnblogs.com/xuzhudong/p/8339853.html

<?php
$http = new Swoole\Http\Server("0.0.0.0", 9501);
$http->on('request', function (\Swoole\Http\Request $request,
                               \Swoole\Http\Response $response) {
    $str = "hello world2";
    $etag = md5($str);
    $response->header("Etag", $etag);
    if (isset($request->header["if-none-match"]) && $request->header["if-none-match"] == $etag) {
        $response->status(304);
        $response->end();
    }else{
        $response->end($str);
    }

});
$http->start();

Last-Modified

<?php
$http = new Swoole\Http\Server("0.0.0.0", 9501);
$http->on('request', function (\Swoole\Http\Request $request,
                               \Swoole\Http\Response $response) {
    $last_modified_time = filemtime(__DIR__."/test.txt");
    $last_modified_time = gmdate('D, d M Y H:i:s',$last_modified_time)." GMT";
    $response->header("Last-Modified", $last_modified_time);
    if (isset($request->header["if-modified-since"]) && $request->header["if-modified-since"] == $last_modified_time) {
        $response->status(304);
        $response->end();
    }else{
        $response->end(file_get_contents(__DIR__."/test.txt"));
    }

});
$http->start();
<?php
$http = new Swoole\Http\Server("0.0.0.0", 9501);
$http->set([
    "enable_static_handler"=>true,
    "document_root"=>realpath(__DIR__)
]);
$http->on('request', function (\Swoole\Http\Request $request,
                               \Swoole\Http\Response $response) {
    $last_modified_time = filemtime(__DIR__."/test.txt");
    $last_modified_time = gmdate('D, d M Y H:i:s',$last_modified_time)." GMT";
    $response->header("Last-Modified", $last_modified_time);
    if (isset($request->header["if-modified-since"]) && $request->header["if-modified-since"] == $last_modified_time) {
        $response->status(304);
        $response->end();
    }else{
        $response->end(file_get_contents(__DIR__."/test.txt"));
    }

});
$http->start();

未用gzip 24923 level1:9714 level2:9481 level9:8862

$index = 3;
$timer_id = Swoole\Timer::tick(1000, function() use(&$index){
    echo "timeout{$index}\n";
    global $timer_id;
    if(!--$index){
        Swoole\Timer::clear($timer_id);
    }
});