C#佇列學習筆記:RabbitMQ實現客戶端相互通訊
阿新 • • 發佈:2020-04-12
原文:C#佇列學習筆記:RabbitMQ實現客戶端相互通訊
一、引言
fanout型別的Exchange,路由規則非常簡單:它會把所有傳送到該Exchange的訊息,路由到所有與它繫結的Queue中。假設有一個聊天室,各個客戶端都訂閱在同一fanout exchange type,那每個客戶端傳送出來的訊息,所有的客戶端都能收到,因為大家都訂閱了。此時,只需要簡單地限制一下,只有是與我有關的訊息,才在聊天介面上顯示。這樣,即可達到相互通訊的效果。
二、示例
2.1、環境準備
本示例使用EasyNetQ來實現,請先在NuGet上安裝。
2.2、實體類
新建一個實體類MessageBody:
public class MessageBody { public string FromUserId { get; set; } public string Message { get; set; } public string ToUserId { get; set; } }
2.3、主窗體
新建一個ChatMain窗體:
程式碼如下:
public partial class ChatMain : Form { public ChatMain() { InitializeComponent(); }View Code/// <summary> /// 客戶端 A /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Button1_Click(object sender,EventArgs e) { ChatWith chatWith = new ChatWith(currentUserId: "UserA") { StartPosition= FormStartPosition.CenterScreen }; chatWith.Show(); } /// <summary> /// 客戶端 B /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Button2_Click(object sender,EventArgs e) { ChatWith chatWith = new ChatWith(currentUserId: "UserB") { StartPosition = FormStartPosition.CenterScreen }; chatWith.Show(); } /// <summary> /// 客戶端 C /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Button3_Click(object sender,EventArgs e) { ChatWith chatWith = new ChatWith(currentUserId: "UserC") { StartPosition = FormStartPosition.CenterScreen }; chatWith.Show(); } /// <summary> /// 客戶端 D /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Button4_Click(object sender,EventArgs e) { ChatWith chatWith = new ChatWith(currentUserId: "UserD") { StartPosition = FormStartPosition.CenterScreen }; chatWith.Show(); } }
2.4、客戶端窗體
新建一個ChatWith窗體:
程式碼如下:
public partial class ChatWith : Form { public delegate void ChatWithDelegate(); public delegate void ChatWithDelegate<T1>(T1 obj1); public delegate void ChatWithDelegate<T1,T2>(T1 obj1,T2 obj2); public string CurrentUserId { get; } private IBus bus; public const string ConnStringMQ = "host=192.168.2.242:5672,192.168.2.165:5672;virtualHost=/;username=hello;password=world"; public const string FanoutExchange = "fanoutEC"; /// <summary> /// 有參建構函式 /// </summary> /// <param name="currentUserId"></param> public ChatWith(string currentUserId) { InitializeComponent(); //在多執行緒程式中,新建立的執行緒不能訪問UI執行緒建立的視窗控制元件。 //此時若想訪問窗體的控制元件,可將窗體建構函式中的CheckForIllegalCrossThreadCalls設定為false。 //這時執行緒就能安全地訪問窗體控制元件了。 CheckForIllegalCrossThreadCalls = false; CurrentUserId = currentUserId; } /// <summary> /// ShowMessage過載 /// </summary> /// <param name="msg"></param> private void ShowMessage(string msg) { if (InvokeRequired)//InvokeRequired:當前執行緒不是建立控制元件的執行緒時為true { BeginInvoke(new ChatWithDelegate<string>(ShowMessage),msg); } else { ListViewItem item = new ListViewItem(new string[] { DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),msg }); lvwReceiveMsg.Items.Insert(0,item); } } /// <summary> /// ShowMessage過載 /// </summary> /// <param name="toUserId"></param> /// <param name="msg"></param> private void ShowMessage(string toUserId,string msg) { if (InvokeRequired) { BeginInvoke(new ChatWithDelegate<string,string>(ShowMessage),toUserId,item); } } /// <summary> /// 繫結佇列並訂閱 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void ChatWith_Load(object sender,EventArgs e) { cmbOnLine.SelectedIndex = 0; Text = Text + $"[{CurrentUserId}]"; //這裡不能使用using,否則訂閱者立即就釋放了,訂閱不到訊息。 bus = RabbitHutch.CreateBus(ConnStringMQ); { if (bus.IsConnected) { var exchange = bus.Advanced.ExchangeDeclare(name: FanoutExchange,type: ExchangeType.Fanout); var queue = bus.Advanced.QueueDeclare(name: $"{FanoutExchange}_queue_{CurrentUserId}"); bus.Advanced.Bind(exchange: exchange,queue: queue,routingKey: ""); bus.Advanced.Consume(queue,registration => { registration.Add<MessageBody>((message,info) => { if (message.Body.ToUserId == CurrentUserId) { ShowMessage(message.Body.FromUserId,message.Body.Message); } }); }); } else { ShowMessage("伺服器連線失敗。"); } } } /// <summary> /// 傳送 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnSend_Click(object sender,EventArgs e) { try { using (var bus = RabbitHutch.CreateBus(ConnStringMQ)) { if (bus.IsConnected) { if (cmbOnLine.Text == "*")//群發 { foreach (var item in cmbOnLine.Items.Cast<string>().Where(s => s != "*" && s != CurrentUserId)) { var exchange = bus.Advanced.ExchangeDeclare(name: FanoutExchange,type: ExchangeType.Fanout); var messageBody = new MessageBody { FromUserId = CurrentUserId,Message = txtSendMsg.Text,ToUserId = item }; bus.Advanced.Publish(exchange: exchange,routingKey: "",mandatory: false,message: new Message<MessageBody>(messageBody)); } } else//私聊 { var exchange = bus.Advanced.ExchangeDeclare(name: FanoutExchange,type: ExchangeType.Fanout); var messageBody = new MessageBody { FromUserId = CurrentUserId,ToUserId = cmbOnLine.Text }; bus.Advanced.Publish(exchange: exchange,message: new Message<MessageBody>(messageBody)); } } else { ShowMessage("傳送訊息失敗。"); } } } catch (Exception ex) { ShowMessage(ex.Message); } } /// <summary> /// 關閉 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnClose_Click(object sender,EventArgs e) { Close(); } /// <summary> /// 窗體關閉事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void ChatWith_FormClosed(object sender,FormClosedEventArgs e) { bus?.Dispose(); } }View Code