mirror of
https://github.com/mainflux/mainflux.git
synced 2025-04-28 13:48:49 +08:00
73 lines
1.9 KiB
HTML
73 lines
1.9 KiB
HTML
<!DOCTYPE html>
|
|
<html>
|
|
<head>
|
|
<meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
|
|
<script src="https://cdnjs.cloudflare.com/ajax/libs/paho-mqtt/1.0.2/mqttws31.min.js" type="text/javascript"></script>
|
|
<script type="text/javascript">
|
|
|
|
//sample HTML/JS script that will publish/subscribe to topics in the Google Chrome Console
|
|
//by Matthew Bordignon @bordignon on twitter.
|
|
|
|
var wsbroker = "localhost"; //mqtt websocket enabled broker
|
|
var wsport = 8883 // port for above
|
|
|
|
var client = new Paho.MQTT.Client(wsbroker, wsport, '/ws');
|
|
|
|
client.onConnectionLost = function (responseObject) {
|
|
console.log("connection lost: " + responseObject.errorMessage);
|
|
};
|
|
|
|
client.onMessageArrived = function (message) {
|
|
console.log("Hello msg", message.destinationName, ' -- ', message.payloadString);
|
|
};
|
|
|
|
var options = {
|
|
timeout: 3,
|
|
onSuccess: function () {
|
|
console.log("mqtt connected");
|
|
// Connection succeeded; subscribe to our topic, you can add multile lines of these
|
|
client.subscribe('mainflux/channels/:your_channel_id/messages/senml-json', {qos: 1}); //Replace <:your_channel_id>
|
|
|
|
//use the below if you want to publish to a topic on connect
|
|
var payload = [
|
|
{
|
|
"bn":"e35b157f-21b8-4adb-ab59-9df21461c815",
|
|
"bt":1.276020076001e+09,
|
|
"bu":"A",
|
|
"bver":5,
|
|
"n":"voltage",
|
|
"u":"V",
|
|
"v":120.1
|
|
},
|
|
{
|
|
"n":"current",
|
|
"t":-5,
|
|
"v":1.2
|
|
},
|
|
{
|
|
"n":"current",
|
|
"t":-4,
|
|
"v":1.3
|
|
}
|
|
];
|
|
message = new Paho.MQTT.Message("Hello from WS");
|
|
message.destinationName = "test";
|
|
// client.send(message);
|
|
|
|
},
|
|
onFailure: function (message) {
|
|
console.log("Connection failed: " + message.errorMessage);
|
|
}
|
|
};
|
|
|
|
function init() {
|
|
client.connect(options);
|
|
}
|
|
|
|
</script>
|
|
</head>
|
|
<body onload="init();">
|
|
</body>
|
|
|
|
</html>
|