. net uses rabbitmq to send instant message demo

Time:2019-11-8

Preface

Recently, rabbitmq will be used in the project. There have been many excellent articles on the Internet. Baidu Encyclopedia also has a very clear explanation of rabbitmq. It is suggested to look at it, and AMQP protocol. It must be mentioned that rabbitmq is an open-source implementation of message queuing protocol (AMQP) provided by lshift, written by Erlang, which is famous for its high performance, robustness and scalability.

Recently, referring to the blog of the great gods, I made a rabbitmq instant messaging demo. Let’s have a look at the detailed introduction.

The steps are as follows: 

1. Use nuget installation package management tool of VS to install rabbitmq.client:

2. Producer Code:

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>
 ///Connection configuration
 /// </summary>
 private static readonly ConnectionFactory rabbitMqFactory = new ConnectionFactory()
 {
  HostName="localhost",
  UserName = "guest",
  Password = "guest",
  Port = 5672,
  //VirtualHost = "JentVirtualHost"
 };
 /// <summary>
 ///Route name
 /// </summary>
 const string ExchangeName = "Jent.Exchange";
 /// <summary>
 ///Queue name
 /// </summary>
 const string QueueName = "Jent.Queue";
 static void Main(string[] args)
 {
  DirectExchangeSendMsg();
  Console. Writeline ("press any key to exit the program! "";
  Console.ReadKey();
 }
 /// <summary>
 ///Single point precise routing mode
 /// </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 ("please enter the message you want to send:");
   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 ("sending time: {0}, after sending, enter exit to exit message sending", datetime. Now. ToString ("yyyy MM DD HH: mm: SS)));
   vadata = Console.ReadLine();
   }
  }
  }
 }
 }
}

3. Consumer code:

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>
 ///Connection configuration
 /// </summary>
 private static readonly ConnectionFactory rabbitMqFactory = new ConnectionFactory()
 {
  HostName = "127.0.0.1",
  UserName = "guest",
  Password = "guest",
  Port = 5672,
  //VirtualHost = "JentVirtualHost"
 };
 /// <summary>
 ///Route name
 /// </summary>
 const string ExchangeName = "Jent.Exchange";
 /// <summary>
 ///Queue name
 /// </summary>
 const string QueueName = "Jent.Queue";

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

  Console. Writeline ("enter any value to exit the program! "";
  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 ("received time: {0}, message content: {1}", datetime. Now. ToString ("yyyy MM DD HH: mm: SS"), msgbody));
   }
   //System.Threading.Thread.Sleep(TimeSpan.FromSeconds(1));
   }
  }
  }
 }
 }
}

4. Program results:

Note: before the first step, you need to install the rabbitmq client, which can be downloaded from http://www.rabbitmq.com/download.html,

However, rabbitmq relies on Erlang OTP platform, so before installing rabbitmq, you need to download and install Erlang from http://www.erlang.org/download.html

For this part, it is recommended to read: https://www.jb51.net/article/143499.htm

summary

The above is the whole content of this article. I hope that the content of this article has a certain reference learning value for everyone’s study or work. If you have any questions, you can leave a message and exchange. Thank you for your support for developepaar.