diff --git a/Tests/MQTTnet.TestApp.AspNetCore2/Startup.cs b/Tests/MQTTnet.TestApp.AspNetCore2/Startup.cs index 56df889..a71978c 100644 --- a/Tests/MQTTnet.TestApp.AspNetCore2/Startup.cs +++ b/Tests/MQTTnet.TestApp.AspNetCore2/Startup.cs @@ -24,14 +24,14 @@ namespace MQTTnet.TestApp.AspNetCore2 app.UseMqttServer(async server => { var msg = new MqttApplicationMessageBuilder() - .WithPayload("Mqtt is Awesome") - .WithTopic("message") - .Build(); + .WithPayload("Mqtt is awesome") + .WithTopic("message"); while (true) { - server.Publish(msg); + server.Publish(msg.Build()); await Task.Delay(TimeSpan.FromSeconds(2)); + msg.WithPayload("Mqtt is still awesome at " + DateTime.Now); } }); diff --git a/Tests/MQTTnet.TestApp.AspNetCore2/wwwroot/Index.html b/Tests/MQTTnet.TestApp.AspNetCore2/wwwroot/Index.html index 522f002..9cc67e4 100644 --- a/Tests/MQTTnet.TestApp.AspNetCore2/wwwroot/Index.html +++ b/Tests/MQTTnet.TestApp.AspNetCore2/wwwroot/Index.html @@ -19,5 +19,17 @@ }); System.import('app/app'); + +
+

+ + + +
+ +
+ +
+ \ No newline at end of file diff --git a/Tests/MQTTnet.TestApp.AspNetCore2/wwwroot/app/app.ts b/Tests/MQTTnet.TestApp.AspNetCore2/wwwroot/app/app.ts index 166a75a..3ce9856 100644 --- a/Tests/MQTTnet.TestApp.AspNetCore2/wwwroot/app/app.ts +++ b/Tests/MQTTnet.TestApp.AspNetCore2/wwwroot/app/app.ts @@ -5,16 +5,52 @@ var client = connect('ws://' + location.host + '/mqtt', clientId: "client1", }); +window.onbeforeunload = () => { + client.end(); +}; + +var publishButton = document.getElementById("publish"); +var topicInput = document.getElementById("topic"); +var msgInput = document.getElementById("msg"); +var stateParagraph = document.getElementById("state"); +var msgsList = document.getElementById("msgs"); + +publishButton.onclick = click => { + var topic = topicInput.value; + var msg = msgInput.value; + client.publish(topic, msg); +}; + client.on('connect', () => { - client.subscribe('presence'); + client.subscribe('#', { qos: 0 }, (err, granted) => { + console.log(err); + }); client.publish('presence', 'Hello mqtt'); + + stateParagraph.innerText = "connected"; +}); + +client.on("error", e => { + showMsg("error: " + e.message); +}); + +client.on("reconnect", () => { + stateParagraph.innerText = "reconnecting"; }); client.on('message', (topic, message) => { - // message is Buffer - console.log(message.toString()); + showMsg(topic + ": " + message.toString()); }); -window.onbeforeunload = () => { - client.end(); -}; \ No newline at end of file +function showMsg(msg: string) { + //console.log(msg); + + var node = document.createElement("LI"); + node.appendChild(document.createTextNode(msg)); + + msgsList.appendChild(node); + + if (msgsList.childElementCount > 20) { + msgsList.removeChild(msgsList.childNodes[0]); + } +} \ No newline at end of file