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. AndroidTv Home界面实现原理(二)——Leanback 库的主页卡位缩放动画源码解析

    先看个效果图: 上一篇中,我们留了问题,在 Tv Home 界面这种很常见聚焦卡位放大动画效果,我们这一篇就来看看 Leanback 库是怎么实现的. 如果要我们自己实现的话,思路应该不难,就是写个放 ...

  2. CSS编码技巧

    前面的话 本文将从DRY.currentColor.inherit和合理使用简写这几方面来详细介绍CSS编码技巧 DRY DRY,即don`t repeat yourself,尽量减少代码重复 在软件 ...

  3. JVM(二)JVM内存布局

    这几天我再次阅读了<深入理解Java虚拟机>之第二章"Java内存区域与内存溢出异常",同时也参考了一些网上的资料,现在把自己的一些认识和体会记录一下.  (本文为博主 ...

  4. 关于selenium IDE找不到元素

    selenium IDE ,明明存在元素,却找不到元素 ,报错Element not found 标签: seleniumselenium IDE自动化测试ide 2016-10-31 13:25 1 ...

  5. 结对编程1 (四则运算基于GUI)

    https://git.coding.net/Luo_yujie/sizeyunsuan.app.git 201421123034 201421123032 1. 需求分析 这次作业新引用了语言选择, ...

  6. 团队作业4——第一次项目冲刺(Alpha版本)第六天and第七天

    团队作业4--第一次项目冲刺(Alpha版本)第六天and第七天 第一次项目冲刺(Alpha版本)第六天 一.Daily Scrum Meeting照片   二.燃尽图 1.解释说明横纵坐标代表的含义 ...

  7. 团队作业4---第一次项目冲刺(ALpha)版本 第五天

    一.Daily Scrum Meeting照片 二.燃尽图 三.项目进展 a.完成所有基础功能 b.正在进行测试调试 四.困难与问题 1.根据测试需求功能,部分基础功能不能实现,性能不达标,后续已完成 ...

  8. 201521123072《Java程序》第二周总结

    201521123072<Java程序>第二周总结 标签(空格分隔): Java学习 [TOC] 1,本周小结 1,字符串的使用, (字符串变量作为对象来处理),所以字符串相等就要用到eq ...

  9. eclipse: eclipse导入工程出现大红叹号

    总结: 问题原因:工程中classpath中指向的包路径错误 解决办法:到BUILDPATH CONFIG````中,liberaies中 出现红色叉号的包为路径错误的包.到classpath中修改相 ...

  10. 实际用户ID,有效用户ID及设置用户ID

    实际用户ID,有效用户ID和设置用户ID 看UNIX相关的书时经常能遇到这几个概念,但一直没有好好去理清这几个概念,以致对这几个概念一直一知半解.今天好好区分了一下这几个概念并总结如下.说白了这几个U ...