一、引言 fanout類型的Exchange,路由規則非常簡單:它會把所有發送到該Exchange的消息,路由到所有與它綁定的Queue中。假設有一個聊天室,各個客戶端都訂閱在同一fanout exchange type,那每個客戶端發送出來的消息,所有的客戶端都能收到,因為大家都訂閱了。此時,只需 ...
一、引言
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(); } /// <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(); } }View Code
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, msg); } else { ListViewItem item = new ListViewItem(new string[] { DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), toUserId, msg }); lvwReceiveMsg.Items.Insert(0, 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, Message = txtSendMsg.Text, ToUserId = cmbOnLine.Text }; bus.Advanced.Publish(exchange: exchange, routingKey: "", mandatory: false, 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