简体   繁体   English

在HTML页面中显示MQTT网络通道数据

[英]Display MQTT webchannel data in a html page

I have to display data of mqtt channel to the html page. 我必须将mqtt通道的数据显示到html页面。 I have prepared a node js code for the same as following : 我已经准备了如下的节点js代码:

var mqtt = require('mqtt');

client = mqtt.createClient(1883, 'mqtt.beebotte.com',
//Authenticate with your channel token,
{username: 'token:TOKEN_KEY', password: ''});


client.on('message', function (topic, message) {
console.log('topic: ' + topic + ' payload: ' + message);
});



client.subscribe('AppTeamDemo/GpsDataUpload');
client.subscribe('AppTeamDemo/EventDataUpload');
client.subscribe('AppTeamDemo/CommToZiggi');
client.subscribe('AppTeamDemo/CommFromZiggi');

And i am able to get data which i need to parse using JSON formatter and have to display it in html page. 而且我能够获取需要使用JSON格式程序解析的数据,并且必须将其显示在html页面中。

I am confused as I am little new to all of this, how would I be able to use nodejs variable to the html page as it is a server side javascript. 我很困惑,因为我对这一切并不陌生,我将如何在HTML页面上使用nodejs变量,因为它是服务器端javascript。

How would I be able to connect with mqtt channel because every time it is saying mqtt is not defined. 因为每次都说未定义mqtt,我将如何与mqtt通道连接。

I preferred not to use node and subscribe this channel in html page by using mqtt and by parsing the JSON data, I want to display latitude and longitude in google map. 我不想使用node并通过使用mqtt并通过解析JSON数据在html页面中订阅此频道,我想在google map中显示纬度和经度。 How would I be able to connect and get JSON response at front-end side ? 我如何能够在前端连接并获取JSON响应?

Please guide me. 请指导我。 Thanks in advance. 提前致谢。

Rather than write your own MQTT to Web bridge have you considered using a MQTT broker that supports WebSockets? 您是否考虑过使用支持WebSocket的MQTT代理,而不是将自己的MQTT编写到Web桥?

If you use the Paho library you can subscribe to the topics you are interested directly from javascript in the page. 如果您使用Paho库,则可以直接从页面中的javascript订阅您感兴趣的主题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM