還沒有鏈接 就報(bào)錯(cuò)了
public function onWorkerStart()
{
var_dump(getenv('MQTT_HOST') . ':' . getenv('MQTT_PORT'));
var_dump(getenv('MQTT_USER') . ':' . getenv('MQTT_PASSWORD'));
$mqtt = new Client(getenv('MQTT_HOST') . ':' . getenv('MQTT_PORT'), [
'username' => getenv('MQTT_USER'),
'password' => getenv('MQTT_PASSWORD'),
]);
}
Fatal error: Declaration of Workerman\Mqtt\Protocols\Mqtt::input(string $buffer, Workerman\Connection\ConnectionInterface $connection): int must be compatible with Workerman\Protocols\ProtocolInterface::input($recv_buffer, Workerman\Connection\ConnectionInterface $connection) in /Users/zhengjiefeng/yujianshanhai/api-device-dept/vendor/workerman/mqtt/src/Protocols/Mqtt.php on line 134
macos
php8.3
workerman/mqtt 2.0
workerman/webman-framework 1.5.19 High performance HTTP Service Framework.
用的docker安裝的,emqx/emqx:5.8.2 鏡像啟動(dòng)的MQTT服務(wù)器
示例代碼
// config/process.php
'mqttclient' => [
'handler' => process\MqttClient::class,
],
//// process/MqttClient.php
<?php
namespace process;
class MqttClient
{
public function onWorkerStart()
{
$mqtt = new \Workerman\Mqtt\Client('mqtt://broker.emqx.io:1883', array(
'debug' => true,
"username"=>"rw", "password"=>"readwrite"
));
$mqtt->onConnect = function($mqtt) {
$mqtt->subscribe('workerman');
};
$mqtt->onMessage = function($topic, $content){
echo "topic:$topic content:$content\n";
};
\Workerman\Timer::add(2, function() use ($mqtt) {
$mqtt->publish('workerman', 'hello workerman mqtt');
});
$mqtt->connect();
}
}
出現(xiàn)錯(cuò)誤信息
Fatal error: Declaration of Workerman\Mqtt\Protocols\Mqtt::input(string $buffer, Workerman\Connection\ConnectionInterface $connection): int must be compatible with Workerman\Protocols\ProtocolInterface::input($recv_buffer, Workerman\Connection\ConnectionInterface $connection) in /Users/xx/webman-test1/vendor/workerman/mqtt/src/Protocols/Mqtt.php on line 134
Worker[73591] process terminated with ERROR: E_COMPILE_ERROR "Declaration of Workerman\Mqtt\Protocols\Mqtt::input(string $buffer, Workerman\Connection\ConnectionInterface $connection): int must be compatible with Workerman\Protocols\ProtocolInterface::input($recv_buffer, Workerman\Connection\ConnectionInterface $connection) in /Users/xx/webman-test1/vendor/workerman/mqtt/src/Protocols/Mqtt.php on line 134"
我想直接發(fā)送主題消息給MQTT,應(yīng)該怎么處理,不開自定義進(jìn)程,不用處理訂閱主題消息
public function test30()
{
try {
$mqtt = new \Workerman\Mqtt\Client('mqtt://broker.emqx.io:1883', array(
'debug' => true,
"username"=>"rw", "password"=>"readwrite"
));
$mqtt->connect();
$data = [
'topic' => 'workerman',
'content' => json_encode(['yl' => 01]),
];
$mqtt->publish($data['topic'], $data['content']);
$mqtt->close();
return $this->success();
} catch (\Exception $e) {
echo "Error: " . $e->getMessage();
}
}
出現(xiàn)報(bào)錯(cuò)
-> Try to connect to broker.emqx.io:1883/, protocol: Workerman\Mqtt\Protocols\Mqtt
-- Error: No connection to broker
Mqtt client: No connection to broker
-> Connection->close() called
-- Connection closed