laravel8集成workerman

优点是 workerman与laravel共享代码, 能够使用laravel强大的功能写workerman

缺点: 速度可能不理想,; 不知道mysql连接使用laravel的数据库组件不知道 会不会断

-------------

首先执行  composer require workerman/workerman  安装workerman

创建command

php artisan make:command WorkermanHttpserver

生成文件:

app/Console/Commands/WorkermanHttpserver.php

修改代码为:

<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Workerman\Worker;
use App;
use Illuminate\Support\Facades\DB;

class WorkermanHttpserver extends Command
{
    protected $httpserver;
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'workerman:httpserver {action} {--daemonize}';
    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'workerman httpserver';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        //因为workerman需要带参数 所以得强制修改
        global $argv;
        $action = $this->argument('action');
        if (!in_array($action, ['start','stop'])) {
            $this->error('Error Arguments');
            exit;
        }
        $argv[0] = 'workerman:httpserver';
        $argv[1] = $action;
        $argv[2] = $this->option('daemonize') ? '-d' : '';
        $this->httpserver = new Worker('http://0.0.0.0:8080');
        // App::instance('workerman:httpserver',$this->httpserver);
        $this->httpserver->onMessage = function ($connection, $data) {
            $user = DB::table('users')->first();
            //$user = json_decode(json_encode($user), 1);
            $connection->send(json_encode($user));
            $connection->send('laravel workerman hello world');
        };
        Worker::runAll();
    }
}

需要在linux下运行, 如何配置lamp环境和怎么安装workerman, 请自行百度, 我的博客里面也有

调试模式运行:

php artisan workerman:httpserver start

后台运行:

php artisan workerman:httpserver status --daemonize

我的是linux虚拟机

浏览器访问:

http://192.168.56.101:8080/

得到的是一个json串

参考:

laravel 整合WorkerMan - 花泪哲 - 博客园

完整示例: 注意分支

tbqlarapps/laravel   分支: tbq-v8.6-workerman1

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值