PHP code example of jose-chan / swoole-laravel

1. Go to this page and download the library: Download jose-chan/swoole-laravel library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

jose-chan / swoole-laravel example snippets



class TestController extends \App\Http\Controllers\Controller
{
    public function fetch(\Illuminate\Http\Request $request)
    {
        // 创建协程
        $coroutine1 = \JoseChan\SwooleLaravel\Utils\AsyncCo::coroutine(function () {
            $c = microtime(true);

            echo "开始查询1\n";
            $orders = \Illuminate\Database\Eloquent\Model::query()->get();
            $d = microtime(true);
            echo "查询1结束", ($d - $c), "\n";
            return $orders;
        });

        $coroutine2 = \JoseChan\SwooleLaravel\Utils\AsyncCo::coroutine(function () {
            $e = microtime(true);
            echo "开始查询2\n";
            $orders = \Illuminate\Database\Eloquent\Model::query()->get();
            $f = microtime(true);
            echo "查询2结束", ($f - $e), "\n";
            return $orders;
        });
        
        // 等待协程执行完成
        $coroutine1->join();
        // 等待协程执行完成,超时跳过等待,等待1s(1000ms)
        $coroutine2->join(1000);
    }
}
`ssh
php artisan vendor:publish --tag=swoole-laravel-server
`
php public/server.php