using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using RabbitMQ.Client;
using RabbitMQ.Client.Events;
namespace MQ_Unity_PLC
{
public partial class MainWindow : Window
{
ConnectionFactory _fac;
IConnection _con;
IModel _ch;
EventingBasicConsumer _consumer;
public MainWindow()
{
InitializeComponent();
_fac = new ConnectionFactory() { HostName = "localhost"};
_con = _fac.CreateConnection();
_ch = _con.CreateModel();
_ch.QueueDeclare(queue: "MasterSendout", false, false, false, null);
_ch.QueueDeclare("MasterReceive", false, false, false, null);
_consumer = new EventingBasicConsumer(_ch);
_consumer.Received += _consumer_Received;
_ch.BasicConsume("MasterReceive", true, _consumer);
}
private void _consumer_Received(object sender, BasicDeliverEventArgs e)
{
var body = e.Body.ToArray();
var message = Encoding.UTF8.GetString(body);
Dispatcher.Invoke(() => txtSendOut.Text = message);
}
private void Button_Click(object sender, RoutedEventArgs e)
{
var body = Encoding.UTF8.GetBytes(txtSendOut.Text);
_ch.BasicPublish("", "MasterSendout", null, body);
}
}
}