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消息推送(三)的更多相关文章

  1. apollo实现c#与android消息推送(二)

    安装完成apache apollo后,org.eclipse.paho是很方便的测试软件,下来介绍paho的安装和使用 2. 搭建paho: a 下载 org.eclipse.paho.ui.app- ...

  2. apollo实现c#与android消息推送(一)

    之前做了c#推送消息到手机端,限于网络要求,不能使用百度等现成的推送,查了许多资料,七拼八凑终于凑齐,记录下来,即是复习也是希望对来者有所帮助. 我开发的环境是windows,使用java开发的Apa ...

  3. apollo实现c#与android消息推送(四)

    4  Android代码只是为了实现功能,比较简单,就只是贴出来 package com.myapps.mqtttest; import java.util.concurrent.Executors; ...

  4. Android消息推送的服务端

    2.Android消息推送 MQTT服务器采用mosquito  http://mosquitto.org/ PHP管理包采用phpmqttclient:https://github.com/toku ...

  5. 采用MQTT协议实现android消息推送(2)MQTT服务端与客户端软件对比、android客户端示列表

    1.服务端软件对比 https://github.com/mqtt/mqtt.github.io/wiki/servers 名称(点名进官网) 特性 简介 收费 支持的客户端语言 IBM MQ 完整的 ...

  6. android 消息推送

    android 消息推送 极光推送百度云推送(语音)友盟消息推送

  7. Android消息推送——JPush极光推送

    刚看了一篇关于Android消息推送评测总结的博客http://www.cnblogs.com/logan/p/4514635.html: 自己也对原学过的JPush极光进行一下小结,方便后续工作使用 ...

  8. Android消息推送完美方案[转]

    转自 Android消息推送完美方案 推送功能在手机应用开发中越来越重要,已经成为手机开发的必须.在Android应用开发中,由于众所周知的原因,Android消息推送我们不得不大费周折.本文就是用来 ...

  9. Android消息推送完美方案

    转自:http://bbs.hiapk.com/thread-4652657-1-1.html 推送功能在手机应用开发中越来越重要,已经成为手机开发的必须.在Android应用开发中,由于众所周知的原 ...

随机推荐

  1. Python练习2

    [之前发布到本人的51cto博客,现转过来] 无意看到老男孩的博文:合格linux运维人员必会的30道shell编程面试题及讲解 http://oldboy.blog.51cto.com/256141 ...

  2. jenkins 每个月1号到7号 一天执行一次

    在线Crontab表达式执行时间验证 / crontab执行时间计算 - aTool在线工具验证 http://www.atool.org/crontab.php 1.Build periodic a ...

  3. 迁移学习-Transfer Learning

    迁移学习两种类型: ConvNet as fixed feature extractor:利用在大数据集(如ImageNet)上预训练过的ConvNet(如AlexNet,VGGNet),移除最后几层 ...

  4. 所有做java开发的都是些垃圾

    所有做java开发的都是些垃圾,再垃圾的框架,只要有人用,对java程序员来说那就是高性能,高可用,解耦的,非常优秀的一款框架.属于吃屎都吃的津津有味.java里的框架都是垃圾,连一个不错的都没有.比 ...

  5. plsql developer 恢复默认布局界面

    tools-preferences-appearance-(reset docking,reset toolbars)

  6. 【找不到与请求 URI匹配的 HTTP 资源】(转)

    在.net下,创建一个HTTP服务,有很多方案,比较老ashx,一般处理程序(HttpHandler), Web Service SOAP协议的,数据格式是XML,HTTP协议         WCF ...

  7. MPLS LDP随堂笔记2

    前一天排错 Acl 1 匹配所有ospf的数据包 (目的 ospf建立邻居关系 传递路由条目) 2 放行UDP报文 让LDP邻居能互相收发HELLO包 4 放行TCP报文 让LDP邻居能够建立TCP会 ...

  8. linux命令每日一练:find与rm实现查找并删除目录或文件

    linux命令每日一练 linux中find与rm实现查找并删除目录或文件 linux 下用find命令查找文件,rm命令删除文件. 删除指定目录下指定文件 find 要查找的目录名 -name .s ...

  9. 201521123102 《Java程序设计》第8周学习总结

    1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结集合与泛型相关内容. 2. 书面作业 本次作业题集集合 1.List中指定元素的删除(题目4-1) 1.1 实验总结 在实验中,Sc ...

  10. 201521123076 《Java程序设计》第7周学习总结

    1. 本周学习总结 2. 书面作业 Q1.ArrayList代码分析 1.1 解释ArrayList的contains源代码 A:先上源代码: public boolean contains(Obje ...