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应用开发中,由于众所周知的原 ...
随机推荐
- Response.Write 、RegisterClientScriptBlock和RegisterStartupScript总结
Response.Write .RegisterClientScriptBlock和RegisterStartupScript总结 Page.ClientScript.RegisterStartupS ...
- [js高手之路]设计模式系列课程-委托模式实战微博发布功能
在实际开发中,经常需要为Dom元素绑定事件,如果页面上有4个li元素,点击对应的li,弹出对应的li内容,怎么做呢?是不是很简单? 大多数人的做法都是:获取元素,绑定事件 <ul> < ...
- [2014-08-18]Mac OSX 命令行快捷键
系统:OSX 10.9.4 将光标移动到行首:ctrl + a 将光标移动到行尾:ctrl + e 清除屏幕: ctrl + l 搜索以前使用命令:ctrl + r 清除当前行: ctrl + u 清 ...
- MySql-python的API手记
--------------------python控制mysql的API--------------------#import MySQLdb:引用对应的开发包#conn=MySQLdb.conne ...
- 带你走进SAP项目实施过程——立项(1)
到底谁会首先有上ERP的想法,可能是企业老板,也可能是总经理级别等高管.但不管是谁,在确定之前,按道理企业风控部.总经办或者信息部等相关部门都需要对ERP项目做立项申请.毕竟ERP项目涉及企业方方面面 ...
- cocos quick lua 输入框点击穿透的问题处理方案。
条件:当前版本quick-3.3 -lua,系统 win7. 问题:在输入框(textField或者editbox,下文"输入框"就代表这两种)打开的情况下弹出其他界面盖住输入框, ...
- 自动化测试:behave
*:first-child { margin-top: 0 !important; } body > *:last-child { margin-bottom: 0 !important; } ...
- centos6.6配置rsync+sersync实现实时同步分布式多客户端分发同步
1.sersync项目: sersync项目利用inotify与rsync技术实现对服务器数据实时同步到解决方案,其中inotify用于监控sersync所在服务器上文件系统的事件变化,rsync是目 ...
- 【集美大学1411_助教博客】团队作业9——测试与发布(Beta版本)
写在前面的话 已经看到了大家的发布成果,很欣喜,虽然有的团队的产品还是有一点问题,但大家也都发布成功了,这就是软件的魅力.但还是要说一些问题,大家录的视频不是没人讲解就是讲得太快,在我看来这都没有在卖 ...
- 201521123061 《Java程序设计》第九周学习总结
201521123061 <Java程序设计>第九周学习总结 1. 本周学习总结 2. 书面作业 本次PTA作业题集异常 1.常用异常 题目5-1 1.1 截图你的提交结果(出现学号) 1 ...