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. private ,friendly,public protected四种修饰符访问权限(从idea代码提示中看出)

    文件一,本类中可以访问全部: package xsf; /** * Created by liwenj on 2017/7/25. */ public class A { private int x= ...

  2. windows平台下node,npm,gulp配置

    参考文献:http://blog.csdn.net/yuanyuan214365/article/details/53749583 1.安装nodejs:nodejs nodejs安装路径随意 nod ...

  3. 带你走进SAP项目实施过程——立项(1)

    到底谁会首先有上ERP的想法,可能是企业老板,也可能是总经理级别等高管.但不管是谁,在确定之前,按道理企业风控部.总经办或者信息部等相关部门都需要对ERP项目做立项申请.毕竟ERP项目涉及企业方方面面 ...

  4. 实战案例--TEMPDB暴涨

    前言   tempdb暴增,造成磁盘空间不足,甚至影响业务运行.     正文   如图,tempdb log文件从7.40开始突然暴涨,因为 tempdb 0 M到 40G     tempdb 所 ...

  5. SQL Server 的锁定和阻塞

    本帖提供两种做法,可避免在 SQL Server 事务锁定时产生的不正常或长时间阻塞,让用户和程序也无限期等待,甚至引起 connection pooling 连接数超过容量. 所谓的「阻塞」,是指当 ...

  6. Flask04 后台获取请求数据、视图函数返回类型、前台接受响应数据

    1 后台获取请求数据 1.1 提出问题 前台发送请求的方式有哪些 后台如何获取这些请求的参数 1.2 前台发送请求的方式 GET.POST.AJAX 点睛:如果不指定请求方式,浏览器默认使用GET请求 ...

  7. 花生壳DDNS为何不支持LetsEncrypt申请

    Inspired by Let's Encrypt Community , thx to sahsanu, jsha, and orzorc. 开端 Lets Encrypt 是一款免费的网站 SSL ...

  8. WCF(一)基础整理

    学习WCF之前,了解下WCF和WebService的区别. WCF和WebService区别 Web Service严格来说是行业标准,也就是Web Service 规范,它使用XML扩展标记语言来表 ...

  9. Spark 贝叶斯分类算法

    一.贝叶斯定理数学基础 我们都知道条件概率的数学公式形式为 即B发生的条件下A发生的概率等于A和B同时发生的概率除以B发生的概率. 根据此公式变换,得到贝叶斯公式:  即贝叶斯定律是关于随机事件A和B ...

  10. 01-TypeScript概述

    本篇文章向大家介绍新的TypeScript客户端脚本语言,主要涉及两个方面,一是传统JavaScript语言的弱点,二是TypeScript语言的优势. 一.JavaScript的弱点 1.弱类型,缺 ...