public bool PublishEvent(EventData eventToPublish) { if (!mqttClient.IsConnected) { return(false); } var s = Newtonsoft.Json.JsonConvert.SerializeObject(eventToPublish); var applicationMessage = new MQTTnet.MqttApplicationMessageBuilder() .WithTopic(eventToPublish.Id) .WithPayload(s) .Build(); mqttClient.PublishAsync(applicationMessage, System.Threading.CancellationToken.None); return(true); }
private void button_Click(object sender, EventArgs e) { if ((mqttClient != null) && (mqttClientConnected)) { Button btn = (Button)sender; if (btn.Name.StartsWith("button")) { ICControlObject obj = new ICControlObject(); int rm = int.Parse(btn.Name.Substring("button".Length)); settingRefObjChannel(ref obj, rm); string topic = String.Format(txTopicFmt, txDevs[rm - 1]); var msg = new MQTTnet.MqttApplicationMessageBuilder() .WithPayload(JsonConvert.SerializeObject(obj)) .WithTopic(topic) .WithAtMostOnceQoS() .Build(); mqttClient.PublishAsync(msg); string json = JsonConvert.SerializeObject(obj); debugOutput("topic", topic); debugOutput("json", json); } } }