zoukankan      html  css  js  c++  java
  • apollo实现c#与android消息推送(三)

    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
  • 相关阅读:
    thinkphp5 tp5 命名空间 报错 Namespace declaration statement has to be the very first statement in the script
    开启 php 错误 提示 php-fpm 重启 nginx 500错误 解决办法 wdlinux lnmp 一键包 php脚本无法解析执行
    js 设置 cookie 定时 弹出层 提示层 下次访问 不再显示 弹窗 getCookie setCookie setTimeout
    php 二维数组 转字符串 implode 方便 mysql in 查询
    nginx 重启 ps -ef|grep nginx kill -HUP 主进程号
    jquery bootstrap help-block input 表单 提示 帮助 信息
    jquery 倒计时 60秒 短信 验证码 js ajax 获取
    jQuery如何获取同一个类标签的所有的值 遍历
    linux下C语言文件操作相关函数
    gcc,gdb用法
  • 原文地址:https://www.cnblogs.com/xlxr45/p/7624205.html
Copyright © 2011-2022 走看看