apollo實現c#與android消息推送(三)
阿新 • • 發佈:2017-10-03
class net names oid urn 服務 mbo target parse
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(objectView Codesender, EventArgs e) { string clientId = ""; string ip = ""; string port = ""; if (String.IsNullOrEmpty(textBoxCT.Text)) { MessageBox.Show("請輸入客戶機標識!"); return; } if (String.IsNullOrEmpty(textBoxAD.Text) && textBoxAD.Text.IndexOf(‘:‘)<=0) { MessageBox.Show("請輸入IP地址且帶端口號!"); return; } clientId = textBoxCT.Text; ip = textBoxAD.Text.Substring(0,textBoxAD.Text.IndexOf(‘:‘)); port = textBoxAD.Text.Substring(textBoxAD.Text.IndexOf(‘:‘)+1);try { client = new MqttClient(IPAddress.Parse(ip), Convert.ToInt32(port), false, null); client.Connect(clientId, "admin", "password", false, 0x01, false, null, null, true, 60);//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("已斷開連接,請重新連接!"); } }View Code
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("已斷開連接,請重新連接!"); } }View Code
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(‘:‘)<=0) { MessageBox.Show("請輸入IP地址且帶端口號!"); return; } clientId = textBoxCT.Text; ip = textBoxAD.Text.Substring(0,textBoxAD.Text.IndexOf(‘:‘)); port = textBoxAD.Text.Substring(textBoxAD.Text.IndexOf(‘:‘)+1); try { client = new MqttClient(IPAddress.Parse(ip), Convert.ToInt32(port), false, null); client.Connect(clientId, "admin", "password", false, 0x01, false, null, null, true, 60);//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(0, 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("已斷開連接,請重新連接!"); } } } }View Code
apollo實現c#與android消息推送(三)