This commit is contained in:
mkm 2023-12-01 10:30:07 +08:00
parent 3a57936b31
commit f991d37953
3 changed files with 32 additions and 31 deletions

View File

@ -1,29 +1,22 @@
<?php <?php
namespace app; namespace app\mqtt;
use Workerman\Worker;
class Publish class Publish
{ {
public static function onWorkerStart() public static function onWorkerStart()
{ {
$mqtt = new \Workerman\Mqtt\Client('mqtt://127.0.0.1:1883', array( $mqtt = new \Workerman\Mqtt\Client('mqtt://ceshi-mqtt.lihaink.cn/tcp', array(
//'debug' => true, "username"=>"demo2",
"username"=>"admin", "password"=>"123456",
"password"=>"admin123456", "client_id"=>"mqttx_4fde83eb"
"client_id"=>"admin"
)); ));
$mqtt->onConnect = function($mqtt) { $mqtt->onConnect = function($mqtt) {
$mqtt->subscribe('test'); $mqtt->publish('demo', 'hello workerman mqtt');
};
$mqtt->onMessage = function($topic, $content) {
echo "topic:$topic content:$content\n";
}; };
// $mqtt->onMessage = function($topic, $content) {
// echo "topic:$topic content:$content\n";
// };
$mqtt->connect(); $mqtt->connect();
} }
} }

View File

@ -1,29 +1,26 @@
<?php <?php
namespace app; namespace app\mqtt;
use Workerman\Worker;
class Subscribe class Subscribe
{ {
public static function onWorkerStart() public static function onWorkerStart()
{ {
$mqtt = new \Workerman\Mqtt\Client('mqtt://127.0.0.1:1883', array( $mqtt = new \Workerman\Mqtt\Client('mqtt://ceshi-mqtt.lihaink.cn/tcp', array(
// 'debug' => true, // 'debug' => true,
"username"=>"admin", "username"=>"demo2",
"password"=>"admin123456", "password"=>"123456",
"client_id"=>"admin" "client_id"=>"mqttx_4fde83eb"
)); ));
$mqtt->onConnect = function($mqtt) { $mqtt->onConnect = function($mqtt) {
$mqtt->publish('test', 'hello workerman mqtt'); $mqtt->subscribe('demo');
};
$mqtt->onMessage = function($topic, $content) {
echo "topic:$topic content:$content\n";
}; };
// $mqtt->onMessage = function($topic, $content) {
// echo "topic:$topic content:$content\n";
// };
$mqtt->connect(); $mqtt->connect();
} }
} }

View File

@ -1,4 +1,5 @@
<?php <?php
/** /**
* This file is part of webman. * This file is part of webman.
* *
@ -38,5 +39,15 @@ return [
'enable_memory_monitor' => DIRECTORY_SEPARATOR === '/', 'enable_memory_monitor' => DIRECTORY_SEPARATOR === '/',
] ]
] ]
] ],
// 'mqtt_push' => [
// 'handler' => app\mqtt\Publish::class,
// 'listen' => 'websocket://0.0.0.0:8955',
// 'count' => 1,
// ],
'mqtt_sub' => [
'handler' => app\mqtt\Subscribe::class,
'listen' => 'websocket://0.0.0.0:8956',
'count' => 1,
],
]; ];