這篇文章主要介紹“Laravel如何整合Workerman命令行監(jiān)聽MQTT”的相關(guān)知識,小編通過實際案例向大家展示操作過程,操作方法簡單快捷,實用性強(qiáng),希望這篇“Laravel如何整合Workerman命令行監(jiān)聽MQTT”文章能幫助大家解決問題。
成都創(chuàng)新互聯(lián)是網(wǎng)站建設(shè)技術(shù)企業(yè),為成都企業(yè)提供專業(yè)的成都做網(wǎng)站、網(wǎng)站建設(shè),網(wǎng)站設(shè)計,網(wǎng)站制作,網(wǎng)站改版等技術(shù)服務(wù)。擁有10多年豐富建站經(jīng)驗和眾多成功案例,為您定制適合企業(yè)的網(wǎng)站。10多年品質(zhì),值得信賴!
現(xiàn)代PHP開發(fā)composer應(yīng)該是必不可少的了,基本上大點的框架都會推薦使用composer,所以這里以composer來安裝Laravel,命令如下:
composer create-project --prefer-dist laravel/laravel workerman-mqtt ‘5.5.*’
Laravel指定版本為5.5.x,這是目前(2019.08)的唯一LTS版本,考慮到企業(yè)項目的穩(wěn)定及安全,還是選擇LTS。項目名稱為workerman-mqtt,專門用這個來測試MQTT。
如果composer太慢,可以考慮使用阿里云等國內(nèi)composer源加快安裝速度。
上面說了,用composer安裝workerman-mqtt很簡單,只需要一行命令:
? cd workerman-mqtt
? composer require workerman/mqtt
Using version ^1.0 for workerman/mqtt
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
Package operations: 2 installs, 0 updates, 0 removals
- Installing workerman/workerman (v3.5.20): Loading from cache
- Installing workerman/mqtt (v1.0): Loading from cache
workerman/workerman suggests installing ext-event (For better performance. )
Package phpunit/phpunit-mock-objects is abandoned, you should avoid using it. No replacement was suggested.
Writing lock file
Generating optimized autoload files
Carbon 1 is deprecated, see how to migrate to Carbon 2.
https://carbon.nesbot.com/docs/#api-carbon-2
You can run './vendor/bin/upgrade-carbon' to get help in updating carbon and other frameworks and libraries that depend on it.
> Illuminate\Foundation\ComposerScripts::postAutoloadDump
> @php artisan package:discover
Discovered Package: fideloper/proxy
Discovered Package: laravel/tinker
Discovered Package: nesbot/carbon
Package manifest generated successfully.
既然是使用Laravel配合workerman監(jiān)聽MQTT,那artisan自然是不二的選擇了,能使用Laravel的組件,還能使用artisan命令管理監(jiān)聽進(jìn)程。創(chuàng)建相關(guān)命令文件:
? php artisan make:command mqtt
Console command created successfully.
然后編輯生成的workerman-mqtt/app/Console/Commands/mqtt.php文件,將文件改成如下內(nèi)容:
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Workerman\Worker;
class mqtt extends Command
{
protected $signature = 'mqtt {action}';
protected $description = 'PHP Server MQTT Client';
protected $client_id = 'php-server';
public function __construct()
{
parent::__construct();
}
public function handle()
{
global $argv;
$arg = $this->argument('action');
$argv [1] = $arg;
$worker = new Worker();
$worker->count = 1;
$worker->onWorkerStart = function () {
$mqtt = new \Workerman\Mqtt\Client("mqtt://" . env('MQTT_HOST') . ":" . env('MQTT_PORT'), array(
// 'ssl' => array(
// 'local_cert' => base_path() . '/path/mqtt/client.crt',
// 'local_pk' => base_path() . '/path/mqtt/client.key',
// 'cafile' => base_path() . '/path/mqtt/ca.crt',
// 'verify_peer' => false,
// 'allow_self_signed' => true,
// ),
// $mqtt->transport = 'ssl';
'username' => env('MQTT_USER'),
'password' => env('MQTT_PASSWORD'),
'debug' => env('MQTT_DEBUG'),
'client_id' => $this->client_id . mt_rand(0, 999),
'will' => [
'topic' => 'status/' . $this->client_id,
'content' => 0,
'qos' => 2,
'retain' => true,
]
));
$mqtt->onConnect = function ($mqtt) {
$mqtt->subscribe('/iot/#');
};
$mqtt->onMessage = function ($topic, $data, $mqtt) {
var_dump($topic);
var_dump($data);
//TODO 業(yè)務(wù)代碼
//publish消息到topic
$mqtt->publish('test', 'hello workerman mqtt');
};
$mqtt->connect();
};
Worker::runAll();
}
}
然后到項目根目錄下面的.env文件添加下面幾項:
MQTT_HOST=mqtt-broker.test
MQTT_PORT=1883
MQTT_USER=username
MQTT_PASSWORD=password
MQTT_DEBUG=true
其中,onConnect里面的subscribe后面是需要監(jiān)聽的topic,當(dāng)收到新消息的時候,onMessage里面的topic是消息的topic,data就是具體的消息信息了,有了這兩個,我們可以在onMessage里面寫我們的業(yè)務(wù)邏輯,當(dāng)然也可以引入Laravel框架本身的一些組件,比如數(shù)據(jù)庫、日志等,也可以與別的服務(wù)比如說redis、消息隊列MQ等配合進(jìn)行緩存或者使用消息隊列等。
和別的artisan命令差不多,直接命令行運行就好了:
? php artisan mqtt start
Workerman[artisan] start in DEBUG mode
------------------------------------- WORKERMAN --------------------------------------
Workerman version:3.5.20 PHP version:7.1.30
-------------------------------------- WORKERS ---------------------------------------
proto user worker listen processes status
tcp zoco none none 1 [OK]
--------------------------------------------------------------------------------------
Press Ctrl+C to stop. Start success.
-> Try to connect to mqtt://mqtt-broker.test:1883
-- Tcp connection established
-> Send CONNECT package client_id:php-server-superuser-subscribe95 username:username password:password clean_session:1 protocol_name:MQTT protocol_level:4
<- Recv CONNACK package, MQTT connect success
-> Send SUBSCRIBE package, topic:/iot/# message_id:1
<- Recv SUBACK package, message_id:1
注意別忘了后面的start,這是workerman本身需要的啟動參數(shù)。
因為workerman設(shè)定是常駐內(nèi)存,正常情況下是在持續(xù)在監(jiān)聽的,即使程序有bug被終止,workerman也會自動新建一個進(jìn)程進(jìn)行處理。
如果生產(chǎn)環(huán)境需要長期監(jiān)聽處理MQTT數(shù)據(jù),建議使用systemctl等命令來管理。
雖然到上面為止已經(jīng)可以作為客戶端在服務(wù)端監(jiān)聽MQTT的消息,但是這里有個不足之處就是,我到目前沒找到辦法單獨調(diào)用這個庫在處理實際業(yè)務(wù)邏輯的時候publish消息到指定的topic。
還有一點是,使用這個庫,不能同時運行兩個使用了這個庫的artisan命令,會有如下提示:
? php artisan mqtt start
Workerman[artisan] start in DEBUG mode
Workerman[artisan] already running
這個問題搜遍了全網(wǎng)也沒發(fā)現(xiàn)有解決的辦法。雖然可以通過Timer類添加定時功能通過另類辦法解決,但是在需要效率的情況下,這并不是最優(yōu)解,如果有別的方案,建議還是不要選擇PHP作為服務(wù)端來處理MQTT相關(guān)業(yè)務(wù)。
關(guān)于“Laravel如何整合Workerman命令行監(jiān)聽MQTT”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識,可以關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,小編每天都會為大家更新不同的知識點。
分享名稱:Laravel如何整合Workerman命令行監(jiān)聽MQTT
網(wǎng)址分享:http://muchs.cn/article22/gjchjc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供App設(shè)計、外貿(mào)網(wǎng)站建設(shè)、網(wǎng)站設(shè)計、網(wǎng)站策劃、用戶體驗、小程序開發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)