1234567891011121314151617181920212223242526272829303132333435363738 |
- <?php
- namespace app\admin\controller;
- use think\Controller;
- use think\Db;
- use think\Log;
- use think\Request;
- use think\Session;
- class Messagepush extends Controller
- {
- public function index()
- {
- vendor('phpMQTT.phpMQTT');
- $server = '47.98.201.73'; // 服务器IP
- $port = 1883; // 服务器端口
- $username = 'usky'; // 用户名
- $password = 'usky'; // 密码
- $client_id = 'pub_' . uniqid();
-
- $mqtt = new \phpMQTT($server, $port, $client_id);
- if ($mqtt->connect(true, NULL, $username, $password)) {
- $topic = 'topic01';
- $msg = 'Hello World!' . date('Y-m-d H:i:s');
- // echo date('Y-m-d H:i:s') . ' SEND [' . $topic . '] ' . $msg . PHP_EOL;
- // qos = 0:仅发一次,不管是否能收到
- // qos = 1:没返回一直发,可能有重复接收
- // qos = 2:保证必须收到,并且不重复
- $mqtt->publish($topic, $msg, 0);
- usleep(100000);
- $mqtt->close();
- } else {
- echo "Time out!\n";
- }
- }
- }
|