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(':')<=)
{
MessageBox.Show("请输入IP地址且带端口号!");
return;
}
clientId = textBoxCT.Text;
ip = textBoxAD.Text.Substring(,textBoxAD.Text.IndexOf(':'));
port = textBoxAD.Text.Substring(textBoxAD.Text.IndexOf(':')+); try
{
client = new MqttClient(IPAddress.Parse(ip), Convert.ToInt32(port), false, null);
client.Connect(clientId, "admin", "password", false, 0x01, false, null, null, true, );//admin和password是之前在apache apollo中设置的用户名和密码
buttonLink.Enabled = false;
buttonLose.Enabled = true;
textBoxLS.ForeColor = Color.RoyalBlue;
textBoxLS.Text = "已连接";
}
catch (Exception ee)
{
MessageBox.Show("无法连接,请确定代理服务器是否启动,IP端口是否正确");
} }
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("已断开连接,请重新连接!");
}
}
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("已断开连接,请重新连接!");
}
}
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(':')<=)
{
MessageBox.Show("请输入IP地址且带端口号!");
return;
}
clientId = textBoxCT.Text;
ip = textBoxAD.Text.Substring(,textBoxAD.Text.IndexOf(':'));
port = textBoxAD.Text.Substring(textBoxAD.Text.IndexOf(':')+); try
{
client = new MqttClient(IPAddress.Parse(ip), Convert.ToInt32(port), false, null);
client.Connect(clientId, "admin", "password", false, 0x01, false, null, null, true, );//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(, 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("已断开连接,请重新连接!");
}
}
}
}
apollo实现c#与android消息推送(三)的更多相关文章
- apollo实现c#与android消息推送(二)
安装完成apache apollo后,org.eclipse.paho是很方便的测试软件,下来介绍paho的安装和使用 2. 搭建paho: a 下载 org.eclipse.paho.ui.app- ...
- apollo实现c#与android消息推送(一)
之前做了c#推送消息到手机端,限于网络要求,不能使用百度等现成的推送,查了许多资料,七拼八凑终于凑齐,记录下来,即是复习也是希望对来者有所帮助. 我开发的环境是windows,使用java开发的Apa ...
- apollo实现c#与android消息推送(四)
4 Android代码只是为了实现功能,比较简单,就只是贴出来 package com.myapps.mqtttest; import java.util.concurrent.Executors; ...
- Android消息推送的服务端
2.Android消息推送 MQTT服务器采用mosquito http://mosquitto.org/ PHP管理包采用phpmqttclient:https://github.com/toku ...
- 采用MQTT协议实现android消息推送(2)MQTT服务端与客户端软件对比、android客户端示列表
1.服务端软件对比 https://github.com/mqtt/mqtt.github.io/wiki/servers 名称(点名进官网) 特性 简介 收费 支持的客户端语言 IBM MQ 完整的 ...
- android 消息推送
android 消息推送 极光推送百度云推送(语音)友盟消息推送
- Android消息推送——JPush极光推送
刚看了一篇关于Android消息推送评测总结的博客http://www.cnblogs.com/logan/p/4514635.html: 自己也对原学过的JPush极光进行一下小结,方便后续工作使用 ...
- Android消息推送完美方案[转]
转自 Android消息推送完美方案 推送功能在手机应用开发中越来越重要,已经成为手机开发的必须.在Android应用开发中,由于众所周知的原因,Android消息推送我们不得不大费周折.本文就是用来 ...
- Android消息推送完美方案
转自:http://bbs.hiapk.com/thread-4652657-1-1.html 推送功能在手机应用开发中越来越重要,已经成为手机开发的必须.在Android应用开发中,由于众所周知的原 ...
随机推荐
- [2015-10-11]tfs2015 vs2013 配置持续集成
今天刚配置完tfs2015+vs2013的持续集成(自动构建+自动发布),记录一下走过的坑. tfs2015和tfs build server是之前其他同事装的,略去不讲,列一下几个坑以及埋坑方法. ...
- C和C++混合编程之 extern “C”的使用
C和C++混合编程之 extern "C"的使用 首先要明白: C++号称是C语言的超集,也确实,从语言的基本语法上,C++是包含所有C语言的语法的,而且C++为了兼容C,连C语言 ...
- 使用ASP.NET Core MVC 和 Entity Framework Core 开发一个CRUD(增删改查)的应用程序
使用ASP.NET Core MVC 和 Entity Framework Core 开发一个CRUD(增删改查)的应用程序 不定时更新翻译系列,此系列更新毫无时间规律,文笔菜翻译菜求各位看官老爷们轻 ...
- 浅析多线程 对象锁和Class锁
一.前言 本来想在另外一篇文章说的,发现可能篇幅有点大,所以还是另开一篇博文来说好了.知识参考<Java多线程编程核心技术>,评价下这本书吧——大量的代码,简单的说明,真像在看博客.不过这 ...
- 从源码分析java.lang.String.isEmpty()
今天在写代码的时候用到了java.lang.String.isEmpty()的这个方法,之前也用过,今天突发奇想,就看了看源码,了解了解它的实现方法,总结出来,大家可以交流交流. 通常情况下,我们使用 ...
- Java+大数据开发——Hadoop集群环境搭建(二)
1. MAPREDUCE使用 mapreduce是hadoop中的分布式运算编程框架,只要按照其编程规范,只需要编写少量的业务逻辑代码即可实现一个强大的海量数据并发处理程序 2. Demo开发--wo ...
- Swift4.0 Array详解
数组的介绍 数组(Array)是一串有序的由相同类型元素构成的集合,数组中的集合元素是有序的,可以重复出现.在Swift中数组类型是Array,是一个泛型集合.数组分成:可变数组和不可变数组,分别使用 ...
- 基础知识(C#语法、数据库SQL Server)回顾与总结
前言 已经有大概一个多月没有更新博客,可能是开始变得有点懒散了吧,有时候想写,但是又需要额外投入更多的时间去学习,感觉精力完全不够用啊,所以为了弥补这一个多月的潜水,决定写一篇,衔接9月未写博客的空缺 ...
- Spring Ioc DI 原理
IOC(DI):其实这个Spring架构核心的概念没有这么复杂,更不像有些书上描述的那样晦涩.Java程序员都知道:java程序中的每个业务逻辑至少需要两个或以上的对象来协作完成,通常,每个对象在使用 ...
- Matlab生成.exe可执行程序
由于在教学过程中需要演示Matlab程序,而教学机又未安装Matlab程序,因此有必要将Matlab程序生成.exe可执行程序,便于直接执行. 在Matlab中提供了Complier,可直接使用. ( ...