var socket = io.connect();
socket.on('connect', function () {
socket.on('mqtt', function (msg) {
var message = msg.topic.split('/');
var area = message[1];
var state = message[2];
console.log(msg.topic, msg.payload);
var timestamp = Math.round((new Date()).getTime() / 1000);
$('#topic').html(msg.topic);
$('#message').html(msg.topic + ', ' + msg.payload);
switch (area) {
case 'living':
$('#value1').html('(Sensor value: ' + msg.payload + ')');
$('#label1').text(msg.payload + '°C');
$('#label1').removeClass('').addClass('label-default');
var entry = new Array();
entry.push(timestamp);
entry.push(parseInt(msg.payload));
Temperature.push(entry);
// Show only 20 values
if (Temperature.length >= 20) {
Temperature.shift()
}
请我尝试在我的网络应用程序上显示我的MQTT数据,并且在我收到消息时似乎不会更新
var cfenv = require('cfenv');
var app = express();
var http=require('http');
var server = require('http').Server(app);
var io = require('socket.io')(server);
var mqtt = require('mqtt');
var MQTT_TOPIC = "IOC/Ke/Data";
var MQTT_ADDR = "mqtt://realtime.ngi.ibm.com";
var MQTT_PORT = 1883;
app.get('/', function (req, res) {
res.sendFile(__dirname + '/public/index.html');
});
//connect to mqtt server
var client = mqtt.connect(MQTT_ADDR,{clientId:'abbanodejs',protocolId:'MQIsdp', protocolVersion:3,connectTimeout:30*1000, debug:true});
//Connect to mqtt topic
client.on('connect', function () {
io.on('connection', function(socket){
socket.on('subscribe', function(data){
console.log('Subscribing to '+data.topic);
socket.join(data.topic);
client.subscribe(data.topic);
client.publish(MQTT_TOPIC, 'Hello mqtt');
});
});
});
// listen to messages coming from the mqtt broker
client.on('message', function (topic, message) {
console.log(topic+'='+message);
io.emit('mqtt',
{'topic':topic,
'payload':message
}
);
});
这是我在另一边的服务器代码,它工作正常并给我我想要的输出