Пример #1
0
        public void Start()
        {
            _connector.AddHost("tmq://localhost:48050");

            _connector.Connected       += Connected;
            _connector.MessageReceived += MessageReceived;
            _connector.Run();

            _timer = new Timer(async o =>
            {
                if (_connector.IsConnected)
                {
                    TmqClient client = _connector.GetClient();

                    TmqMessage message  = new TmqMessage(MessageType.Client, "producer-id");
                    message.ContentType = ModelTypes.ConsumerRequest;

                    ConsumerRequest request = new ConsumerRequest();
                    request.Guid            = Guid.NewGuid().ToString();

                    await message.SetJsonContent(request);
                    TmqMessage response     = await client.Request(message);
                    ProducerResponse rmodel = await response.GetJsonContent <ProducerResponse>();
                    Console.WriteLine($"> response received for: {rmodel.RequestGuid}");
                }
            }, null, 6000, 6000);
        }
Пример #2
0
        public async Task CatchResponseMessages(bool enabled)
        {
            int          port   = enabled ? 42711 : 42712;
            TestMqServer server = new TestMqServer();

            server.Initialize(port);
            server.Start();

            TmqClient client1 = new TmqClient();
            TmqClient client2 = new TmqClient();

            client1.ClientId = "client-1";
            client2.ClientId = "client-2";
            client1.CatchResponseMessages = enabled;

            await client1.ConnectAsync("tmq://localhost:" + port);

            await client2.ConnectAsync("tmq://localhost:" + port);

            Assert.True(client1.IsConnected);
            Assert.True(client2.IsConnected);

            bool responseCaught = false;

            client1.MessageReceived += (c, m) => responseCaught = true;
            client2.MessageReceived += async(c, m) =>
            {
                TmqMessage rmsg = m.CreateResponse();
                rmsg.SetStringContent("Response!");
                await((TmqClient)c).SendAsync(rmsg);
            };

            TmqMessage msg = new TmqMessage(MessageType.Client, "client-2");

            msg.ResponseRequired = true;
            msg.SetStringContent("Hello, World!");

            TmqMessage response = await client1.Request(msg);

            Assert.NotNull(response);
            Assert.Equal(msg.MessageId, response.MessageId);
            Assert.Equal(enabled, responseCaught);
        }
Пример #3
0
        public async Task WithResponse()
        {
            TestMqServer server = new TestMqServer();

            server.Initialize(42603);
            server.Start();

            TmqClient client1 = new TmqClient();
            TmqClient client2 = new TmqClient();

            client1.ClientId        = "client-1";
            client2.ClientId        = "client-2";
            client2.AutoAcknowledge = true;

            await client1.ConnectAsync("tmq://localhost:42603");

            await client2.ConnectAsync("tmq://localhost:42603");

            Assert.True(client1.IsConnected);
            Assert.True(client2.IsConnected);

            client2.MessageReceived += async(c, m) =>
            {
                if (m.Source == "client-1")
                {
                    TmqMessage rmsg = m.CreateResponse();
                    rmsg.ContentType = 123;
                    rmsg.SetStringContent("Hello, World Response!");
                    await((TmqClient)c).SendAsync(rmsg);
                }
            };

            TmqMessage message = new TmqMessage(MessageType.Client, "client-2");

            message.SetStringContent("Hello, World!");

            TmqMessage response = await client1.Request(message);

            Assert.NotNull(response);
            Assert.Equal(123, response.ContentType);
        }