Guest User

Untitled

a guest
Feb 5th, 2018
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.40 KB | None | 0 0
  1. public partial class Form1 : Form
  2. {
  3. public ConnectionFactory ConnectionFactory { get; set; }
  4. public IConnection Connect { get; set; }
  5. public IModel Channel { get; set; }
  6. public bool IsConnectToBus { get; set; }
  7. public bool IsSubscriberNotified { get; set; }// Подписчики оповещены
  8.  
  9. public Form1()
  10. {
  11. InitializeComponent();
  12. }
  13.  
  14. private void btnConnect_Click(object sender, EventArgs e)
  15. {
  16. try
  17. {
  18. var hostName = @"192.168.1.40";
  19. ConnectionFactory = new ConnectionFactory
  20. {
  21. HostName = hostName,
  22. Port = 5672,
  23. UserName = "user",
  24. Password = "password",
  25. VirtualHost = "/",
  26. AutomaticRecoveryEnabled = true,
  27. TopologyRecoveryEnabled = true,
  28. NetworkRecoveryInterval = TimeSpan.FromSeconds(3)
  29. };
  30.  
  31. //Подключение
  32. Connect = ConnectionFactory.CreateConnection();
  33.  
  34. //Создание канала обмена
  35. Channel = Connect.CreateModel();
  36. Channel.ExchangeDeclare(exchange: "dataExchange", type: "direct");
  37.  
  38. IsConnectToBus = Connect.IsOpen;
  39. tb_isConnect.Text = IsConnectToBus.ToString();
  40. }
  41. catch (RabbitMQ.Client.Exceptions.BrokerUnreachableException ex)
  42. {
  43. MessageBox.Show($"Соединение прерванно {ex.ToString()}");
  44. }
  45. catch (Exception ex)
  46. {
  47. MessageBox.Show($"ИСКЛЮЧЕНИЕ {ex.ToString()}");
  48. }
  49. }
  50.  
  51. private void BtnSend_Click(object sender, EventArgs e)
  52. {
  53. var message = tb_message.Text;
  54. if (string.IsNullOrEmpty(message))
  55. {
  56. MessageBox.Show("Mrssage = NULL");
  57. return;
  58. }
  59.  
  60. var routingKey = tb_route.Text;
  61. if (string.IsNullOrEmpty(routingKey))
  62. {
  63. MessageBox.Show("routingKey = NULL");
  64. return;
  65. }
  66.  
  67. try
  68. {
  69. var body = Encoding.UTF8.GetBytes(message);
  70. Channel.BasicPublish(exchange: "dataExchange", routingKey: routingKey, basicProperties: null, body: body);
  71. }
  72. catch (RabbitMQ.Client.Exceptions.BrokerUnreachableException ex)
  73. {
  74. MessageBox.Show($"Соединение прерванно {ex.ToString()}");
  75. }
  76. catch (Exception ex)
  77. {
  78. MessageBox.Show($"ИСКЛЮЧЕНИЕ {ex.ToString()}");
  79. }
  80. }
  81.  
  82.  
  83.  
  84. private void btn_DisConnect_Click(object sender, EventArgs e)
  85. {
  86. Channel?.Close(200, "Goodbye");
  87. Connect?.Close();
  88. }
  89.  
  90.  
  91. protected override void OnClosed(EventArgs e)
  92. {
  93. Channel?.Close(200, "Goodbye");
  94. Connect?.Close();
  95. base.OnClosed(e);
  96. }
  97. }
  98.  
  99. public partial class Form1 : Form
  100. {
  101. public ConnectionFactory ConnectionFactory { get; set; }
  102. public IConnection Connect { get; set; }
  103. public IModel Channel { get; set; }
  104. public bool IsConnectToBus { get; set; }
  105. public bool IsSubscriberNotified { get; set; }// Подписчики оповещенны
  106.  
  107. public Form1()
  108. {
  109. InitializeComponent();
  110. }
  111.  
  112. private void btnConnect_Click(object sender, EventArgs e)
  113. {
  114. try
  115. {
  116. var hostName = @"192.168.1.40";
  117. ConnectionFactory = new ConnectionFactory
  118. {
  119. HostName = hostName,
  120. Port = 5672,
  121. UserName = "user",
  122. Password = "password",
  123. VirtualHost = "/",
  124. AutomaticRecoveryEnabled = true,
  125. TopologyRecoveryEnabled = true,
  126. NetworkRecoveryInterval = TimeSpan.FromSeconds(3)
  127. };
  128.  
  129. //Подключение
  130. Connect = ConnectionFactory.CreateConnection();
  131.  
  132. //Создание канала обмена
  133. Channel = Connect.CreateModel();
  134. Channel.ExchangeDeclare(exchange: "dataExchange", type: "direct");
  135. var queueName = Channel.QueueDeclare().QueueName;
  136. Channel.QueueBind(queue: queueName, exchange: "dataExchange", routingKey: tb_route.Text);
  137.  
  138. //Подписка на событие получения данных
  139. var consumer = new EventingBasicConsumer(Channel);
  140. consumer.Received += Consumer_Received;
  141. Channel.BasicConsume(queue: queueName, autoAck: true, consumer: consumer);
  142.  
  143. IsConnectToBus = Connect.IsOpen;
  144. tb_isConnect.Text = IsConnectToBus.ToString();
  145. }
  146. catch (RabbitMQ.Client.Exceptions.BrokerUnreachableException ex)
  147. {
  148. MessageBox.Show($"Соединение прерванно {ex.ToString()}");
  149. }
  150. catch (Exception ex)
  151. {
  152. MessageBox.Show($"ИСКЛЮЧЕНИЕ {ex.ToString()}");
  153. }
  154. }
  155.  
  156. private void Consumer_Received(object model, BasicDeliverEventArgs ea)
  157. {
  158. var body = ea.Body;
  159. var message = Encoding.UTF8.GetString(body);
  160. var routingKey = ea.RoutingKey;
  161. tb_ReciveData.Text = message;
  162. }
  163.  
  164. private void btn_DisConnect_Click(object sender, EventArgs e)
  165. {
  166. Channel?.Close(200, "Goodbye");
  167. Connect?.Close();
  168. }
  169.  
  170.  
  171. protected override void OnClosed(EventArgs e)
  172. {
  173. Channel?.Close(200, "Goodbye");
  174. Connect?.Close();
  175. base.OnClosed(e);
  176. }
  177. }
Add Comment
Please, Sign In to add comment