代码初始化
This commit is contained in:
31
extend/Mqtt/Mqtt.php
Normal file
31
extend/Mqtt/Mqtt.php
Normal file
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
use think\Env;
|
||||
|
||||
require_once 'phpMQTT.php';
|
||||
class Mqtt {
|
||||
public $mqtt;
|
||||
|
||||
public function __construct() {
|
||||
$this->server = Env::get('mqtt.mqtt_server'); // change if necessary
|
||||
$this->port = Env::get('mqtt.mqtt_port'); // change if necessary
|
||||
$this->username = Env::get('mqtt.mqtt_username'); // set your username
|
||||
$this->password = Env::get('mqtt.mqtt_password'); // set your password
|
||||
}
|
||||
|
||||
public function publishs($topic, $content) {
|
||||
$client_id = 'php-' . uniqid();
|
||||
|
||||
$this->mqtt = new phpMQTT($this->server, $this->port, $client_id);
|
||||
|
||||
if($this->mqtt->connect()) {
|
||||
$this->mqtt->publish($topic, $content, 0);
|
||||
}else{
|
||||
\Think\Log::write('MQTT连接失败: ' . var_export($this->mqtt->connect(), true), 'INFO');
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user