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

相关文章: