代码改变世界

apollo实现c#与android消息推送(三)

2017-10-03 18:28  一截生长  阅读(1434)  评论(0编辑  收藏  举报

3 实现c#消息推送服务

     c#实现消息推送必须引入M2Mqtt.dll,源码

     a 连接apache apollo代理服务器的代码。需要引入using uPLibrary.Networking.M2Mqtt和 using uPLibrary.Networking.M2Mqtt.Messages,需要注意的是代码里的client.Connect里的admin和password是之前设置apache apollo时设置的用户名,密码,请自行修改。apollo实现c#与android消息推送(一)

 //连接apache apollo
        private void LinkClick(object sender, EventArgs e)
        {
            string clientId = "";
            string ip = "";
            string port = "";
            if (String.IsNullOrEmpty(textBoxCT.Text))
            {
                MessageBox.Show("请输入客户机标识!");
                return;
            }
            if (String.IsNullOrEmpty(textBoxAD.Text) && textBoxAD.Text.IndexOf(':')<=0)
            {
                MessageBox.Show("请输入IP地址且带端口号!");
                return;
            }
            clientId = textBoxCT.Text;
            ip = textBoxAD.Text.Substring(0,textBoxAD.Text.IndexOf(':'));
            port = textBoxAD.Text.Substring(textBoxAD.Text.IndexOf(':')+1);

            try
            {
                client = new MqttClient(IPAddress.Parse(ip), Convert.ToInt32(port), false, null);
                client.Connect(clientId, "admin", "password", false, 0x01, false, null, null, true, 60);//admin和password是之前在apache apollo中设置的用户名和密码
                buttonLink.Enabled = false;
                buttonLose.Enabled = true;
                textBoxLS.ForeColor = Color.RoyalBlue;
                textBoxLS.Text = "已连接";
            }
            catch (Exception ee)
            {
                MessageBox.Show("无法连接,请确定代理服务器是否启动,IP端口是否正确");
            }

        }
View Code

     b 发布主题代码

private void PublishSubmit(object sender, EventArgs e)
        {
            string pubTitle = "";//发布主题
            byte pubQu ;//发布质量

            if (String.IsNullOrEmpty(textBoxPuIt.Text))
            {
                MessageBox.Show("请输入发布主题!");
                return;
            }
            if (String.IsNullOrEmpty(comboBoxPub.Text))
            {
                MessageBox.Show("请选择发布主题质量!");
                return;
            }
            pubTitle = textBoxPuIt.Text;

            if (comboBoxPub.Text=="至多一次")
            {
                pubQu = MqttMsgBase.QOS_LEVEL_AT_MOST_ONCE;
            }
            else if (comboBoxPub.Text == "至少一次")
            {
                pubQu = MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE;
            }
            else
            {
                pubQu = MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE;
            }
            try
            {
                topic = pubTitle;
                client.Subscribe(new string[] { pubTitle }, new byte[] { pubQu });
                client.Publish(pubTitle, Encoding.UTF8.GetBytes(richTextBoxMess.Text), MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE,
                       false);
            }
            catch (Exception)
            {
                if (client!=null)
                {
                    client = null;
                }
                MessageBox.Show("已断开连接,请重新连接!");
            }
        }
View Code

    c  订阅主题代码

private void SubClick(object sender, EventArgs e)
        {
            string subTitle = "";//发布主题
            byte subQu;//发布质量
            if (String.IsNullOrEmpty(textBoxSub.Text))
            {
                MessageBox.Show("请输入订阅主题!");
                return;
            }
            if (String.IsNullOrEmpty(comboBoxSub.Text))
            {
                MessageBox.Show("请选择订阅主题质量!");
                return;
            }
            subTitle = textBoxSub.Text;
            if (comboBoxSub.Text == "至多一次")
            {
                subQu = MqttMsgBase.QOS_LEVEL_AT_MOST_ONCE;
            }
            else if (comboBoxSub.Text == "至少一次")
            {
                subQu = MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE;
            }
            else
            {
                subQu = MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE;
            }
            try
            {
                buttonRE.Enabled = false;
                buttonca.Enabled = true;
                topic = subTitle;
                client.Subscribe(new string[] { subTitle }, new byte[] { subQu });
                client.MqttMsgPublishReceived += client_MqttMsgPublishReceived;
            }
            catch (Exception)
            {
                if (client != null)
                {
                    client = null;
                }
                MessageBox.Show("已断开连接,请重新连接!");
            }
        }
View Code

    d  全部代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using uPLibrary.Networking.M2Mqtt;
using uPLibrary.Networking.M2Mqtt.Messages;

namespace MqttCsharp
{
    public partial class Form1 : Form
    {
        private  MqttClient client;
        private string topic = "";

        private delegate void MessDelegate<T>(T obj);
        public Form1()
        {
            InitializeComponent();
            buttonLose.Enabled = false;
            buttonca.Enabled = false;
        }

        //连接apache apollo
        private void LinkClick(object sender, EventArgs e)
        {
            string clientId = "";
            string ip = "";
            string port = "";
            if (String.IsNullOrEmpty(textBoxCT.Text))
            {
                MessageBox.Show("请输入客户机标识!");
                return;
            }
            if (String.IsNullOrEmpty(textBoxAD.Text) && textBoxAD.Text.IndexOf(':')<=0)
            {
                MessageBox.Show("请输入IP地址且带端口号!");
                return;
            }
            clientId = textBoxCT.Text;
            ip = textBoxAD.Text.Substring(0,textBoxAD.Text.IndexOf(':'));
            port = textBoxAD.Text.Substring(textBoxAD.Text.IndexOf(':')+1);

            try
            {
                client = new MqttClient(IPAddress.Parse(ip), Convert.ToInt32(port), false, null);
                client.Connect(clientId, "admin", "password", false, 0x01, false, null, null, true, 60);//admin和password是之前在apache apollo中设置的用户名和密码
                buttonLink.Enabled = false;
                buttonLose.Enabled = true;
                textBoxLS.ForeColor = Color.RoyalBlue;
                textBoxLS.Text = "已连接";
            }
            catch (Exception ee)
            {
                MessageBox.Show("无法连接,请确定代理服务器是否启动,IP端口是否正确");
            }

        }
        //断开apache apollo连接
        private void CloseLink(object sender, EventArgs e)
        {
            if (client !=null && client.IsConnected)
            {
                client.Disconnect();
                client = null;
                buttonLink.Enabled = true;
                buttonLose.Enabled = false;
                textBoxLS.ForeColor = Color.Firebrick;
                textBoxLS.Text = "断开连接";
            }

        }
        //发布按钮的点击事件
        private void PublishSubmit(object sender, EventArgs e)
        {
            string pubTitle = "";//发布主题
            byte pubQu ;//发布质量

            if (String.IsNullOrEmpty(textBoxPuIt.Text))
            {
                MessageBox.Show("请输入发布主题!");
                return;
            }
            if (String.IsNullOrEmpty(comboBoxPub.Text))
            {
                MessageBox.Show("请选择发布主题质量!");
                return;
            }
            pubTitle = textBoxPuIt.Text;

            if (comboBoxPub.Text=="至多一次")
            {
                pubQu = MqttMsgBase.QOS_LEVEL_AT_MOST_ONCE;
            }
            else if (comboBoxPub.Text == "至少一次")
            {
                pubQu = MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE;
            }
            else
            {
                pubQu = MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE;
            }
            try
            {
                topic = pubTitle;
                client.Subscribe(new string[] { pubTitle }, new byte[] { pubQu });
                client.Publish(pubTitle, Encoding.UTF8.GetBytes(richTextBoxMess.Text), MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE,
                       false);
            }
            catch (Exception)
            {
                if (client!=null)
                {
                    client = null;
                }
                MessageBox.Show("已断开连接,请重新连接!");
            }
        }
        //订阅按钮
        private void SubClick(object sender, EventArgs e)
        {
            string subTitle = "";//发布主题
            byte subQu;//发布质量
            if (String.IsNullOrEmpty(textBoxSub.Text))
            {
                MessageBox.Show("请输入订阅主题!");
                return;
            }
            if (String.IsNullOrEmpty(comboBoxSub.Text))
            {
                MessageBox.Show("请选择订阅主题质量!");
                return;
            }
            subTitle = textBoxSub.Text;
            if (comboBoxSub.Text == "至多一次")
            {
                subQu = MqttMsgBase.QOS_LEVEL_AT_MOST_ONCE;
            }
            else if (comboBoxSub.Text == "至少一次")
            {
                subQu = MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE;
            }
            else
            {
                subQu = MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE;
            }
            try
            {
                buttonRE.Enabled = false;
                buttonca.Enabled = true;
                topic = subTitle;
                client.Subscribe(new string[] { subTitle }, new byte[] { subQu });
                client.MqttMsgPublishReceived += client_MqttMsgPublishReceived;
            }
            catch (Exception)
            {
                if (client != null)
                {
                    client = null;
                }
                MessageBox.Show("已断开连接,请重新连接!");
            }
        }
        void client_MqttMsgPublishReceived(object sender, MqttMsgPublishEventArgs e)
        {
            //处理接收到的消息
            string msg = Encoding.UTF8.GetString(e.Message);
            ShowMess(msg);
        }
        //列表显示
        private void ShowMess(string msg)
        {
            if (InvokeRequired)
            {
                BeginInvoke(new MessDelegate<string>(ShowMess), msg);
            }
            else
            {
                ListViewItem item = new ListViewItem(new string[]
                {
                    topic,msg,DateTime.Now.ToString()
                });

                listViewMess.Items.Insert(0, item);
            }
           
        }

        private void CancelSubClick(object sender, EventArgs e)
        {
            try
            {
                if (client.IsConnected)
                {
                    client.Unsubscribe(new string[] { topic });
                }
                buttonRE.Enabled = true;
                buttonca.Enabled = false;
            }
            catch (Exception)
            {
                if (client != null)
                {
                    client = null;
                }
                MessageBox.Show("已断开连接,请重新连接!");
            }
        }
    }
}
View Code