欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

mqtt向android推送消息(一)——发送端使用.net

程序员文章站 2022-01-18 10:31:27
...

 

使用.net进行mqtt协议通讯,主要是为了开发居于mqtt协议向android推送消息,使用.net开发可以参考mqtt.org上面的库,我用的是M2MQTT,网上资料很少,具体例子如下:
一、消息发布:
if (this.sender == null)
            {
                this.sender = new MqttClient(IPAddress.Parse(host), 1883);
                //var mqttClient = new MqttClient("localhost");
                this.sender.Connect("sender");
            }
            this.sender.Publish("mobileGKTopic", System.Text.Encoding.Default.GetBytes(textBox2.Text));
 二、消息接收
 mqttClient = new MqttClient(IPAddress.Parse(host), 1883);
            //mqttClient = new MqttClient("localhost");
            mqttClient.Connect("Receiver");
            mqttClient.MqttMsgPublishReceived += new MqttClient.MqttMsgPublishEventHandler(mqttClient_MqttMsgPublishReceived);

            

            mqttClient.Subscribe(new string[] { "testTopic" }, new byte[] { MqttMsgBase.QOS_LEVEL_AT_LEAST_ONCE });

private void mqttClient_MqttMsgPublishReceived(object sender, uPLibrary.Networking.M2Mqtt.Messages.MqttMsgPublishEventArgs e)
        {
            //throw new NotImplementedException();
            var msg = System.Text.Encoding.Default.GetString(e.Message);
            if (!String.IsNullOrEmpty(msg))
            {
                textBox1.AppendText(Environment.NewLine);
                textBox1.AppendText(msg);
            }
        }
 需要注意的是,编译mqtt库时要把条件编译SSL关闭,SSL的编译我还没有高清。

mqtt向android推送消息(一)——发送端使用.net
            
    
    博客分类: .Net安卓 android.netc# 
 
  • mqtt向android推送消息(一)——发送端使用.net
            
    
    博客分类: .Net安卓 android.netc# 
  • 大小: 19.4 KB
相关标签: android .net c#