dpj83664 2016-10-24 15:05
浏览 1294
已采纳

保持MQTT客户端连接始终处于活动状态

I am using CloudMQTT as a MQTT broker in my Pub-Sub based application. I am using my publisher to publish data to the CloudMQTT server over a topic, and I plan to subscribe to the broker on my webpage to recieve the transmitted information.

I am using this procedure to create a Client(subscriber): https://www.cloudmqtt.com/docs-php.html

Code goes as follows:

// subscribe.php
require("phpMQTT.php");

$host = "hostname"; 
$port = port;
$username = "username"; 
$password = "password"; 

$mqtt = new phpMQTT($host, $port, "ClientID".rand()); 

if(!$mqtt->connect(true,NULL,$username,$password)){
  exit(1);
}

//currently subscribed topics
$topics['topic'] = array("qos"=>0, "function"=>"procmsg");
$mqtt->subscribe($topics,0);

while($mqtt->proc()){        
}

$mqtt->close();
function procmsg($topic,$msg){
  echo "Msg Recieved: $msg";
}

Here is the phpMQTT.php file: https://github.com/bluerhinos/phpMQTT/blob/master/phpMQTT.php

However, the issue in this case is that it recieves data only when the webpage is open.. I want to keep the connection alive even if the webpage is not open to always recieve published messages, how can I do it?

EDIT : I might be open to using some other technology on the server to handle this subscription process, if anyone can recommend some alternatives

  • 写回答

1条回答

      报告相同问题?

      相关推荐 更多相似问题

      悬赏问题

      • ¥30 Dijkstra‘s 最短路径
      • ¥15 在线教育培训平台,主要以点播视频和在线练习测试为主,除将视频做切片外,有其他哪种方式降低流量?(关键词-带宽速率)
      • ¥20 用c语音或c++实现银行叫号系统
      • ¥15 人工智能 规则正向演绎和推理
      • ¥20 基于STM32F401的电子密码锁设计
      • ¥15 famamacbeth回归中遇到only size-1 arrays can be converted to Python scalars,求解答
      • ¥15 单片机多个自锁按键的编程实践
      • ¥15 用python操作redis存储中文后,再取出的数据变成了乱码怎么办?
      • ¥15 C语言简单排序问题有偿求解
      • ¥20 请问图片的代码什么意思