Advertisement
vitareinforce

Ping Pong Messaging

Feb 22nd, 2019
420
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.85 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using RabbitMQ.Client;
  7. using System.Diagnostics;
  8. using RabbitMQ.Client.Events;
  9.  
  10. namespace PingPongMessaging_cmd
  11. {
  12.     class RMQ
  13.     {
  14.         public ConnectionFactory connectionFactory;
  15.         public IConnection connection;
  16.         public IModel channel;
  17.  
  18.         public void InitRMQConnection(string host = "localhost", int port = 5672, string user = "guest", string pass = "guest", string vhost = "/")
  19.         {
  20.             connectionFactory = new ConnectionFactory();
  21.             connectionFactory.HostName = host;
  22.             connectionFactory.Port = port;
  23.             connectionFactory.UserName = user;
  24.             connectionFactory.Password = pass;
  25.             connectionFactory.VirtualHost = vhost;
  26.         }
  27.  
  28.         public void CreateRMQConnection()
  29.         {
  30.             connection = connectionFactory.CreateConnection();
  31.             Console.WriteLine("Koneksi " + (connection.IsOpen ? "Berhasil!" : "Gagal!"));
  32.         }
  33.  
  34.         public void CreateRMQChannel(string queue_name,string routingKey = "", string exchange_name = "")
  35.         {
  36.             if (connection.IsOpen)
  37.             {
  38.                 channel = connection.CreateModel();
  39.                 Console.WriteLine("Channel " + (channel.IsOpen ? "Berhasil!" : "Gagal!"));
  40.             }
  41.  
  42.             if (channel.IsOpen)
  43.             {
  44.                 channel.QueueDeclare(queue: queue_name,
  45.                                      durable: true,
  46.                                      exclusive: false,
  47.                                      autoDelete: false,
  48.                                      arguments: null);
  49.                 Console.WriteLine("Queue telah dideklarasikan..");
  50.             }
  51.         }
  52.  
  53.         public void WaitingMessage(string queue_name)
  54.         {
  55.             if (channel.IsOpen)
  56.             {
  57.                 channel.QueueDeclare(queue: queue_name,
  58.                                      durable: true,
  59.                                      exclusive: false,
  60.                                      autoDelete: false,
  61.                                      arguments: null);
  62.  
  63.                 var consumer = new EventingBasicConsumer(channel);
  64.                 string tujuan, inputMsg;
  65.  
  66.                 Console.WriteLine("Type 'q' to quit, 's' to send message, or any other key to start listening.");
  67.                 string cmd = Console.ReadLine();
  68.                
  69.  
  70.                 consumer.Received += (model, ea) =>
  71.                 {
  72.                     var body = ea.Body;
  73.                     var message = Encoding.UTF8.GetString(body);
  74.                     Console.WriteLine(" [x] Pesan diterima: {0}", message);
  75.                     Console.WriteLine("Type 'q' to quit, press another key to send message");
  76.                     cmd = Console.ReadLine();
  77.                     if (cmd == "q")
  78.                     {
  79.                         Disconnect();
  80.                     }
  81.                     else
  82.                     {
  83.                         Console.Write("Masukkan pesan yang akan dikirim atau 'exit' to close.\n>>");
  84.                         Console.Write(">> tujuan: ");
  85.                         tujuan = Console.ReadLine();
  86.                         Console.Write(">> pesan: ");
  87.                         inputMsg = Console.ReadLine();
  88.                         SendMessage(tujuan, inputMsg);
  89.                     }
  90.                    
  91.                 };
  92.                 channel.BasicConsume(queue: queue_name,
  93.                                      autoAck: true,
  94.                                      consumer: consumer);
  95.                 if (cmd == "q")
  96.                 {
  97.                     Disconnect();
  98.                 }else if(cmd == "s")
  99.                 {
  100.                     Console.Write("Masukkan pesan yang akan dikirim atau 'exit' to close.\n>>");
  101.                     Console.Write(">> tujuan: ");
  102.                     tujuan = Console.ReadLine();
  103.                     Console.Write(">> pesan: ");
  104.                     inputMsg = Console.ReadLine();
  105.                     SendMessage(tujuan, inputMsg);
  106.                 }
  107.             }
  108.         }
  109.  
  110.         public void SendMessage(string tujuan, string msg = "send")
  111.         {
  112.             byte[] responseBytes = Encoding.UTF8.GetBytes(msg);// konversi pesan dalam bentuk string menjadi byte
  113.            
  114.             channel.BasicPublish(exchange: "",
  115.                                     routingKey: tujuan,
  116.                                     basicProperties: null,
  117.                                     body: responseBytes);
  118.             Console.WriteLine("Pesan: '"+msg+"' telah dikirim.");
  119.         }
  120.  
  121.         public void Disconnect()
  122.         {
  123.             channel.Close();
  124.             channel = null;
  125.             Console.WriteLine("Channel ditutup!");
  126.             if (connection.IsOpen)
  127.             {
  128.                 connection.Close();
  129.             }
  130.  
  131.             Console.WriteLine("Koneksi diputus!");
  132.             connection.Dispose();
  133.             connection = null;
  134.         }
  135.     }
  136.  
  137.     class Program
  138.     {
  139.  
  140.         static void Main(string[] args)
  141.         {
  142.             RMQ rmq = new RMQ();
  143.             Console.WriteLine("Tekan tombol apapun untuk inisialisasi RMQ parameters.");
  144.             Console.ReadKey();
  145.             rmq.InitRMQConnection(); // inisialisasi parameter (secara default) untuk koneksi ke server RMQ
  146.             Console.WriteLine("Tekan tombol apapun untuk membuka koneksi ke RMQ.");
  147.             Console.ReadKey();
  148.             rmq.CreateRMQConnection(); // memulai koneksi dengan RMQ
  149.             Console.Write("Masukkan nama queue channel untuk mengirim pesan melalui RMQ.\n>> ");
  150.             string queue_name = Console.ReadLine();
  151.             rmq.CreateRMQChannel(queue_name);
  152.            
  153.             rmq.WaitingMessage(queue_name);
  154.            
  155.         }        
  156.     }
  157. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement