溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

怎么在.Net中使用RabbitMQ發(fā)送即時消息

發(fā)布時間:2021-03-26 17:14:11 來源:億速云 閱讀:250 作者:Leah 欄目:開發(fā)技術(shù)

怎么在.Net中使用RabbitMQ發(fā)送即時消息?相信很多沒有經(jīng)驗的人對此束手無策,為此本文總結(jié)了問題出現(xiàn)的原因和解決方法,通過這篇文章希望你能解決這個問題。

步驟如下: 

1.使用VS的NuGet安裝包管理工具安裝RabbitMQ.Client:

怎么在.Net中使用RabbitMQ發(fā)送即時消息

2.生產(chǎn)者端代碼:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using RabbitMQ.Client;

namespace RabbitMQ.Producter
{
 class Program
 {
 /// <summary>
 /// 連接配置
 /// </summary>
 private static readonly ConnectionFactory rabbitMqFactory = new ConnectionFactory()
 {
  HostName="localhost",
  UserName = "guest",
  Password = "guest",
  Port = 5672,
  //VirtualHost = "JentVirtualHost"
 };
 /// <summary>
 /// 路由名稱
 /// </summary>
 const string ExchangeName = "Jent.Exchange";
 /// <summary>
 /// 隊列名稱
 /// </summary>
 const string QueueName = "Jent.Queue";
 static void Main(string[] args)
 {
  DirectExchangeSendMsg();
  Console.WriteLine("按任意鍵退出程序!");
  Console.ReadKey();
 }
 /// <summary>
 /// 單點精確路由模式
 /// </summary>
 private static void DirectExchangeSendMsg()
 {
  using (IConnection conn = rabbitMqFactory.CreateConnection())
  {
  using (IModel channel = conn.CreateModel())
  {
   channel.ExchangeDeclare(ExchangeName, "direct", durable: true, autoDelete: false, arguments: null);
   channel.QueueDeclare(QueueName, durable: true, exclusive: false, autoDelete: false, arguments: null);
   channel.QueueBind(QueueName, ExchangeName, routingKey: QueueName);

   var props = channel.CreateBasicProperties();
   props.Persistent = true;
   Console.WriteLine("請輸入需要發(fā)送的消息:");
   string vadata = Console.ReadLine();
   while (vadata != "exit")
   {
   var msgBody = Encoding.UTF8.GetBytes(vadata);
   channel.BasicPublish(exchange: ExchangeName, routingKey: QueueName, basicProperties: props, body: msgBody);
   Console.WriteLine(string.Format("發(fā)送時間:{0},發(fā)送完畢,輸入exit退出消息發(fā)送", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")));
   vadata = Console.ReadLine();
   }
  }
  }
 }
 }
}

3.消費者端代碼:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using RabbitMQ.Client;

namespace RabbitMQ.Consumer
{
 class Program
 {
 /// <summary>
 /// 連接配置
 /// </summary>
 private static readonly ConnectionFactory rabbitMqFactory = new ConnectionFactory()
 {
  HostName = "127.0.0.1",
  UserName = "guest",
  Password = "guest",
  Port = 5672,
  //VirtualHost = "JentVirtualHost"
 };
 /// <summary>
 /// 路由名稱
 /// </summary>
 const string ExchangeName = "Jent.Exchange";
 /// <summary>
 /// 隊列名稱
 /// </summary>
 const string QueueName = "Jent.Queue";

 static void Main(string[] args)
 {
  DirectAcceptExchange();

  Console.WriteLine("輸入任意值退出程序!");
  Console.ReadKey();
 }

 private static void DirectAcceptExchange()
 {
  using (IConnection conn = rabbitMqFactory.CreateConnection())
  {
  using (IModel channel = conn.CreateModel())
  {
   channel.ExchangeDeclare(ExchangeName, "direct", durable: true, autoDelete: false, arguments: null);
   channel.QueueDeclare(QueueName, durable: true, exclusive: false, autoDelete: false, arguments: null);
   channel.QueueBind(QueueName, ExchangeName, routingKey: QueueName);

   while (true)
   {
   BasicGetResult msgResponse = channel.BasicGet(QueueName, autoAck: false);
   if (msgResponse != null)
   {
    var msgBody = Encoding.UTF8.GetString(msgResponse.Body);
    Console.WriteLine(string.Format("接收時間:{0},消息內(nèi)容:{1}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), msgBody));
   }
   //System.Threading.Thread.Sleep(TimeSpan.FromSeconds(1));
   }
  }
  }
 }
 }
}

4.程序結(jié)果:

怎么在.Net中使用RabbitMQ發(fā)送即時消息

看完上述內(nèi)容,你們掌握怎么在.Net中使用RabbitMQ發(fā)送即時消息的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI