Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public partial class Form1 : Form
- {
- public ConnectionFactory ConnectionFactory { get; set; }
- public IConnection Connect { get; set; }
- public IModel Channel { get; set; }
- public bool IsConnectToBus { get; set; }
- public bool IsSubscriberNotified { get; set; }// Подписчики оповещены
- public Form1()
- {
- InitializeComponent();
- }
- private void btnConnect_Click(object sender, EventArgs e)
- {
- try
- {
- var hostName = @"192.168.1.40";
- ConnectionFactory = new ConnectionFactory
- {
- HostName = hostName,
- Port = 5672,
- UserName = "user",
- Password = "password",
- VirtualHost = "/",
- AutomaticRecoveryEnabled = true,
- TopologyRecoveryEnabled = true,
- NetworkRecoveryInterval = TimeSpan.FromSeconds(3)
- };
- //Подключение
- Connect = ConnectionFactory.CreateConnection();
- //Создание канала обмена
- Channel = Connect.CreateModel();
- Channel.ExchangeDeclare(exchange: "dataExchange", type: "direct");
- IsConnectToBus = Connect.IsOpen;
- tb_isConnect.Text = IsConnectToBus.ToString();
- }
- catch (RabbitMQ.Client.Exceptions.BrokerUnreachableException ex)
- {
- MessageBox.Show($"Соединение прерванно {ex.ToString()}");
- }
- catch (Exception ex)
- {
- MessageBox.Show($"ИСКЛЮЧЕНИЕ {ex.ToString()}");
- }
- }
- private void BtnSend_Click(object sender, EventArgs e)
- {
- var message = tb_message.Text;
- if (string.IsNullOrEmpty(message))
- {
- MessageBox.Show("Mrssage = NULL");
- return;
- }
- var routingKey = tb_route.Text;
- if (string.IsNullOrEmpty(routingKey))
- {
- MessageBox.Show("routingKey = NULL");
- return;
- }
- try
- {
- var body = Encoding.UTF8.GetBytes(message);
- Channel.BasicPublish(exchange: "dataExchange", routingKey: routingKey, basicProperties: null, body: body);
- }
- catch (RabbitMQ.Client.Exceptions.BrokerUnreachableException ex)
- {
- MessageBox.Show($"Соединение прерванно {ex.ToString()}");
- }
- catch (Exception ex)
- {
- MessageBox.Show($"ИСКЛЮЧЕНИЕ {ex.ToString()}");
- }
- }
- private void btn_DisConnect_Click(object sender, EventArgs e)
- {
- Channel?.Close(200, "Goodbye");
- Connect?.Close();
- }
- protected override void OnClosed(EventArgs e)
- {
- Channel?.Close(200, "Goodbye");
- Connect?.Close();
- base.OnClosed(e);
- }
- }
- public partial class Form1 : Form
- {
- public ConnectionFactory ConnectionFactory { get; set; }
- public IConnection Connect { get; set; }
- public IModel Channel { get; set; }
- public bool IsConnectToBus { get; set; }
- public bool IsSubscriberNotified { get; set; }// Подписчики оповещенны
- public Form1()
- {
- InitializeComponent();
- }
- private void btnConnect_Click(object sender, EventArgs e)
- {
- try
- {
- var hostName = @"192.168.1.40";
- ConnectionFactory = new ConnectionFactory
- {
- HostName = hostName,
- Port = 5672,
- UserName = "user",
- Password = "password",
- VirtualHost = "/",
- AutomaticRecoveryEnabled = true,
- TopologyRecoveryEnabled = true,
- NetworkRecoveryInterval = TimeSpan.FromSeconds(3)
- };
- //Подключение
- Connect = ConnectionFactory.CreateConnection();
- //Создание канала обмена
- Channel = Connect.CreateModel();
- Channel.ExchangeDeclare(exchange: "dataExchange", type: "direct");
- var queueName = Channel.QueueDeclare().QueueName;
- Channel.QueueBind(queue: queueName, exchange: "dataExchange", routingKey: tb_route.Text);
- //Подписка на событие получения данных
- var consumer = new EventingBasicConsumer(Channel);
- consumer.Received += Consumer_Received;
- Channel.BasicConsume(queue: queueName, autoAck: true, consumer: consumer);
- IsConnectToBus = Connect.IsOpen;
- tb_isConnect.Text = IsConnectToBus.ToString();
- }
- catch (RabbitMQ.Client.Exceptions.BrokerUnreachableException ex)
- {
- MessageBox.Show($"Соединение прерванно {ex.ToString()}");
- }
- catch (Exception ex)
- {
- MessageBox.Show($"ИСКЛЮЧЕНИЕ {ex.ToString()}");
- }
- }
- private void Consumer_Received(object model, BasicDeliverEventArgs ea)
- {
- var body = ea.Body;
- var message = Encoding.UTF8.GetString(body);
- var routingKey = ea.RoutingKey;
- tb_ReciveData.Text = message;
- }
- private void btn_DisConnect_Click(object sender, EventArgs e)
- {
- Channel?.Close(200, "Goodbye");
- Connect?.Close();
- }
- protected override void OnClosed(EventArgs e)
- {
- Channel?.Close(200, "Goodbye");
- Connect?.Close();
- base.OnClosed(e);
- }
- }
Add Comment
Please, Sign In to add comment