我是MQTT和node.js的新手,所以如果我没有弄清楚所有条款的正确性,也请理解所有部分是如何组合在一起的,请原谅我
我试图效仿这个例子:http://blog.hekkers.net/2012/10/13/realtime-data-with-mqtt-node-js-mqtt-js-and-socket-io/
我似乎无法做到正确,html页面永远不会显示主题的值/有效负载。
据我了解,node.js文件和html文件有两个文件。对于node.js文件,我安装了所需的模块:socket.io和MQTTClient.js。我的node.js文件 - pusher.js:
var sys = require('sys');
var net = require('net');
var mqtt = require('./node_mqtt_client/MQTTClient.js');
var io = require('socket.io').listen(5000);
var client = new mqtt.MQTTClient(1883, '192.168.1.108', 'pusher');
io.sockets.on('connection', function (socket) {
socket.on('subscribe', function (data) {
console.log('Subscribing to '+data.topic);
client.subscribe(data.topic);
});
});
client.addListener('mqttData', function(topic, payload){
sys.puts(topic+'='+payload);
io.sockets.emit('mqtt',{'topic':String(topic),'payload':String(payload)});
});
108 IP是RabbitMQ Broker的IP。
似乎加载正常,当我访问html文件时,控制台会输出以下信息:
info - socket.io started
Connected as :pusher
Session opend
debug - client authorized
info - handshake authorized FAxJSO4DUEXV5wi50E-k
debug - setting request GET /socket.io/1/websocket/FAxJSO4DUEXV5wi50E-k
debug - set heartbeat interval for client FAxJSO4DUEXV5wi50E-k
debug - client authorized for
debug - websocket writing 1::
Subscribing to aujen/weather/current/pressure
Subcribe to:aujen/weather/current/pressure
Connection closed by broker
info - transport end (undefined)
debug - set close timeout for client FAxJSO4DUEXV5wi50E-k
debug - cleared close timeout for client FAxJSO4DUEXV5wi50E-k
debug - cleared heartbeat interval for client FAxJSO4DUEXV5wi50E-k
debug - discarding transport
debug - client authorized
info - handshake authorized AqzQ60WhMDA1_Ddv0E-l
debug - setting request GET /socket.io/1/websocket/AqzQ60WhMDA1_Ddv0E-l
debug - set heartbeat interval for client AqzQ60WhMDA1_Ddv0E-l
debug - client authorized for
debug - websocket writing 1::
Subscribing to aujen/weather/current/pressure
MQTT connect to server time out
debug - emitting heartbeat for client AqzQ60WhMDA1_Ddv0E-l
debug - websocket writing 2::
debug - set heartbeat timeout for client AqzQ60WhMDA1_Ddv0E-l
debug - got heartbeat packet
对于html部分,我已经安装了apache来托管页面 - 普通的apache没有额外的模块。我还下载了所需的库文件,并将它们放在与我的html文件相同的文件夹中:socket.io.min.js和jquery-1.4.2.js。我的html文件如下:index.html
<h1>Real Time</h1>
<script type="text/javascript" src="socket.io.min.js"></script>
<script type="text/javascript" src="jquery-1.4.2.js"></script>
<script type="text/javascript">
var socket = io.connect('http://192.168.1.106:5000');
socket.on('connect', function () {
socket.on('mqtt', function (msg) {
var elmarr=msg.topic.split("/");
var elm=elmarr[3];
console.log(msg.topic+' '+msg.payload);
$('#'.concat(elm)).html(msg.payload);
});
socket.emit('subscribe',{topic:'aujen/weather/current/pressure'});
});
</script>
<table class="tablegv" style="width: 500px;">
<tbody>
<tr class="tablegvHeader">
<td colspan="2"><center>Status</center></td>
</tr>
<tr>
<td>Return temp</td>
<td id="pressure"></td>
</tr>
</tbody>
</table>
106 IP是我的node.js服务器的IP。
html页面不显示值/有效负载?主题,也不是pusher.js(不确定是否应该)
我知道我的经纪人(RabbitMQ)正在工作,因为我还练习创建一个订阅主题的python脚本,并显示值/有效负载?。
python mqtt_sub.py
rc: 0
Subscribed: 1 (0,)
aujen/weather/current/pressure 0 856.64
aujen/weather/current/temperature 0 25.63
aujen/weather/current/humidity 0 37
aujen/weather/current/pressure 0 856.67
aujen/weather/current/temperature 0 25.63
aujen/weather/current/humidity 0 36
aujen/weather/current/pressure 0 856.59
aujen/weather/current/temperature 0 25.62
aujen/weather/current/humidity 0 37
我似乎无法找到许多尝试使用此方法与代理集成的示例,大多数示例从js文件调用mosquitto_sub客户端 - 对此不太热衷。
希望有人可以指出我做错了什么。 非常感谢你。
答案 0 :(得分:4)
我在使用RabbitMQ + MQTT插件时遇到了同样的错误。 在我将MQTT经纪人改为mosquitto后,没关系。
我尝试将MQTT客户端更改为node.js(npm install mqtt)的mqtt插件,而不是node_mqtt_client / MQTTClient.js。 当我使用它时,我可以使用RabbitMQ + MQTT插件而不会出错。 但在这种情况下,我需要稍微改变一下pusher.js。
var sys = require('sys');
var net = require('net');
var mqtt = require('mqtt');
var io = require('socket.io').listen(5000);
var client = mqtt.createClient(1883, '127.0.0.1');
client.options.reconnectPeriod = 0;
client.on('message', function(topic, message) {
console.log(message);
sys.puts(topic+'='+message);
io.sockets.in(topic).emit('mqtt',{'topic': String(topic), 'payload':String(message)});
});
io.sockets.on('connection', function (socket) {
socket.on('subscribe', function (data) {
console.log('Subscribing to '+data.topic);
socket.join(data.topic);
client.subscribe(data.topic);
});
});