Exemplo n.º 1
0
 private async void OnDirectMessage(DmMessage msg)
 {
     if (msg.Action == EntityAction.Create)
     {
         var vm = await CreateViewModel(msg.DirectMessage);
         await AddItem(vm);
     }
 }
Exemplo n.º 2
0
        void Client_MqttMsgPublishReceived(object sender, MqttMsgPublishEventArgs e)
        {
            try
            {
                Console.WriteLine("Message Published");

                //DmMessage dmMessage = null;
                string ipAddress;
                commBroker = new CommBroker();

                byte[]     publishMessageByte;
                MqttClient client = (MqttClient)sender;
                ipAddress = client.RemoteEndPoint.ToString().Substring(8,
                                                                       (client.RemoteEndPoint.ToString().IndexOf(']') - 8));
                //publishMessageString = Convert.ToBase64String(e.Message) + ipAddress;
                //publishMessageByte = Encoding.UTF8.GetBytes(publishMessageString); //Encoding.ASCII.GetBytes(publishMessageString);
                DmMessage dmMessage = new DmMessage();
                dmMessage.publicIpAddress = ipAddress;
                dmMessage.data            = Convert.ToBase64String(e.Message);
                dmMessage.ToString();
                var publishMessageString = Newtonsoft.Json.JsonConvert.SerializeObject(dmMessage);
                publishMessageByte = Encoding.UTF8.GetBytes(publishMessageString.ToString());

                CommMessage message = new CommMessage()
                {
                    subject        = e.Topic.Replace("/", "."),
                    data           = publishMessageString,
                    data_length    = publishMessageString.Length,
                    message_type   = "string",
                    correlation_id = "",
                    reply_to       = ""
                };

                // create PUBLISH message to publish
                // [v3.1.1] DUP flag from an incoming PUBLISH message is not propagated to subscribers
                //          It should be set in the outgoing PUBLISH message based on transmission for each subscriber
                //MqttMsgPublish publish = new MqttMsgPublish(e.Topic, publishMessageByte, false, e.QosLevel, e.Retain);

                // publish message through publisher manager
                //this.publisherManager.Publish(publish);
                commBroker.connect(e.Topic);
                MqttCommon.LogMessageToFile("Connected to RabbitMQ");
                commBroker.publish(message, false);
                MqttCommon.LogMessageToFile("Topic Name:" + message.subject.ToString());
                MqttCommon.LogMessageToFile("Message Published:" + publishMessageString.ToString());
                MqttCommon.LogMessageToFile("Message Published");
                commBroker.disconnect();
            }
            catch (Exception ex) { MqttCommon.LogMessageToFile("Error:Message Publish - Failed"); }
        }