コード例 #1
0
        public MasterClient()
        {
            logger.Info("Server started");
            ConnectionFactory factory = new ConnectionFactory()
            {
                UserName = "******", Password = "******", HostName = "localhost"
            };

            connection = factory.CreateConnection();
            channel    = connection.CreateModel();
            channel.QueueDeclare(queue: "MasterClient", durable: false, exclusive: false, autoDelete: false, arguments: null);
            //channel.BasicQos(0, 1, false);
            var consumer = new EventingBasicConsumer(channel);

            channel.BasicConsume(queue: "MasterClient", autoAck: true, consumer: consumer);
            consumer.Received += (model, ea) =>
            {
                IBasicProperties props = ea.BasicProperties;
                if (!ClientsList.Exists((c) => c.QeueuName == props.ReplyTo))
                {
                    ClientsList.Add(new ClientPeer()
                    {
                        QeueuName = props.ReplyTo, LastUpTime = DateTime.UtcNow
                    });
                    logger.Info($"Was added a client: {props.ReplyTo}");
                }
                Object obtained = ea.Body.Serializer();
                switch (obtained)
                {
                case JsonClass j:
                    logger.Info("JsonClass");
                    JsonModel jsonModel = new JsonModel()
                    {
                        IP = "localhost", UserName = "******", Password = "******"
                    };
                    string    jsonString = JsonConvert.SerializeObject(jsonModel, Formatting.Indented);
                    JsonClass jsonClass  = new JsonClass()
                    {
                        JsonByteArray = jsonString.Serializer()
                    };
                    channel.BasicPublish(exchange: "", routingKey: props.ReplyTo, basicProperties: null, body: jsonClass.Serializer());

                    break;

                case ImageClass i:
                    logger.Info("ImageClass");
                    ImageClass image = new ImageClass()
                    {
                        ImageByteArray = GetImage()
                    };
                    channel.BasicPublish(exchange: "", routingKey: props.ReplyTo, basicProperties: null, body: image.Serializer());
                    break;

                case FileClass f:
                    logger.Info("FileClass");
                    FileClass file = new FileClass()
                    {
                        FileByteArray = GetFile()
                    };
                    channel.BasicPublish(exchange: "", routingKey: props.ReplyTo, basicProperties: null, body: file.Serializer());
                    break;

                default:
                    logger.Error("Type is different!");
                    break;
                } //switch
            };
        }         //ctor
コード例 #2
0
        public void GiveMeJson()
        {
            JsonClass jsonData = new JsonClass();

            channel.BasicPublish(exchange: "", routingKey: "MasterClient", basicProperties: props, body: jsonData.Serializer());
        }