workemran/mqtt,这个异步消息组件,能否跨越进程去使用?

bobshipwood

问题描述

 $mqtt = new mqtt('mqtt://test.mosquitto.org:1883')
 $this->startWorker1(); // 开启1号workerman, 主要处理http类
 $this->startWorker2(); // 开启2号workerman, 主要处理物联网类(tcp,mqtt)
 Worker::runAll(); // 统一启动所有的worker

public function startWorker1()
    {
        $worker = new Worker();
        $worker->count = 4;
        $worker->onWorkerStart = function ($worker) {
            try {
                switch ($worker->id) {
                    case 0: // 捷顺的任务
                        $jieShun = app()->make(\App\Services\JieShunService::class);
                        //$jieShun->test();
                        break;
                    case 1:
                        break;
                    default:
                        break;
                }
            } catch (\Exception $e) {
//                Log::error("An error occurred in Worker_" . $worker->id . ": " . $e->getMessage(), [ // 记录错误日志
//                    'worker_id' => $worker->id,
//                    'exception' => $e,
//                ]);
                echo "An error occurred in Worker_" . $worker->id . ": " . $e->getMessage();
            }
        };
    }

为此你搜索到了哪些方案及不适用的原因

想请问下这样写行不行

251 1 0
1个回答

walkor 打赏

每个进程要初始化自己的mqtt客户端,不能共用一个,数据会混乱。
不能在启动脚本里直接初始化mqtt客户端,mqtt客户端最好放在onWorkerStart里初始化

×
🔝