Selaa lähdekoodia

added some ui to send test msgs

release/3.x.x
Jan Eggers 7 vuotta sitten
vanhempi
commit
2cd3bd7588
3 muutettua tiedostoa jossa 58 lisäystä ja 10 poistoa
  1. +4
    -4
      Tests/MQTTnet.TestApp.AspNetCore2/Startup.cs
  2. +12
    -0
      Tests/MQTTnet.TestApp.AspNetCore2/wwwroot/Index.html
  3. +42
    -6
      Tests/MQTTnet.TestApp.AspNetCore2/wwwroot/app/app.ts

+ 4
- 4
Tests/MQTTnet.TestApp.AspNetCore2/Startup.cs Näytä tiedosto

@@ -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);
}
});



+ 12
- 0
Tests/MQTTnet.TestApp.AspNetCore2/wwwroot/Index.html Näytä tiedosto

@@ -19,5 +19,17 @@
});
System.import('app/app');
</script>
<div>
<p id="state"></p>
<input id="topic" placeholder="topic" />
<input id="msg" placeholder="msg" />
<button id="publish">send</button>
</div>

<div>
<ul id="msgs" ></ul>
</div>

</body>
</html>

+ 42
- 6
Tests/MQTTnet.TestApp.AspNetCore2/wwwroot/app/app.ts Näytä tiedosto

@@ -5,16 +5,52 @@ var client = connect('ws://' + location.host + '/mqtt',
clientId: "client1",
});

window.onbeforeunload = () => {
client.end();
};

var publishButton = document.getElementById("publish");
var topicInput = <HTMLInputElement>document.getElementById("topic");
var msgInput = <HTMLInputElement>document.getElementById("msg");
var stateParagraph = document.getElementById("state");
var msgsList = <HTMLUListElement>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();
};
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]);
}
}

Ladataan…
Peruuta
Tallenna