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

View File

@ -1,29 +1,26 @@
<?php
namespace app;
use Workerman\Worker;
namespace app\mqtt;
class Subscribe
{
public static function onWorkerStart()
{
$mqtt = new \Workerman\Mqtt\Client('mqtt://127.0.0.1:1883', array(
//'debug' => true,
"username"=>"admin",
"password"=>"admin123456",
"client_id"=>"admin"
$mqtt = new \Workerman\Mqtt\Client('mqtt://ceshi-mqtt.lihaink.cn/tcp', array(
// 'debug' => true,
"username"=>"demo2",
"password"=>"123456",
"client_id"=>"mqttx_4fde83eb"
));
$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();
}
}

View File

@ -1,4 +1,5 @@
<?php
/**
* This file is part of webman.
*
@ -38,5 +39,15 @@ return [
'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,
],
];