Messagepush.php 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. <?php
  2. namespace app\admin\controller;
  3. use think\Controller;
  4. use think\Db;
  5. use think\Log;
  6. use think\Request;
  7. use think\Session;
  8. class Messagepush extends Controller
  9. {
  10. public function index()
  11. {
  12. vendor('phpMQTT.phpMQTT');
  13. $server = '47.98.201.73'; // 服务器IP
  14. $port = 1883; // 服务器端口
  15. $username = 'usky'; // 用户名
  16. $password = 'usky'; // 密码
  17. $client_id = 'pub_' . uniqid();
  18. $mqtt = new \phpMQTT($server, $port, $client_id);
  19. if ($mqtt->connect(true, NULL, $username, $password)) {
  20. $topic = 'topic01';
  21. $msg = 'Hello World!' . date('Y-m-d H:i:s');
  22. // echo date('Y-m-d H:i:s') . ' SEND [' . $topic . '] ' . $msg . PHP_EOL;
  23. // qos = 0:仅发一次,不管是否能收到
  24. // qos = 1:没返回一直发,可能有重复接收
  25. // qos = 2:保证必须收到,并且不重复
  26. $mqtt->publish($topic, $msg, 0);
  27. usleep(100000);
  28. $mqtt->close();
  29. } else {
  30. echo "Time out!\n";
  31. }
  32. }
  33. }