public async Task RoutingTestForCustomSubscriber()
        {
            var address = "tcp://localhost:6001";

            var publisherSocket = new PublisherSocket();

            publisherSocket.Bind(address);

            var subscriberSocket = new SubscriberSocket();

            subscriberSocket.Connect(address);

            var router = NetmqMessageRouter
                         .WithPubSubConnecton(publisherSocket, subscriberSocket)
                         .RegisterJsonSerializer()
                         .RegisterRoute("VectorRoute", typeof(Vector))
                         .RegisterRoute("VectorLengthRoute", typeof(double));

            router
            .Subscribe("VectorRoute")
            .WithResponse("VectorLengthRoute")
            .WithHandler((Vector vector) =>
            {
                return(Math.Sqrt(vector.X * vector.X + vector.Y * vector.Y));
            });

            double?lengthAnswer = null;

            router
            .Subscribe("VectorLengthRoute")
            .WithHandler((double x) => lengthAnswer = x);

            router.StartRouting();

            router
            .SendMessage(new Vector()
            {
                X = 3, Y = 4
            })
            .To("VectorRoute");

            await Task.Delay(TimeSpan.FromSeconds(5));

            router
            .StopRouting()
            .Disconnect();

            Assert.AreEqual(5d, lengthAnswer);
        }
Beispiel #2
0
        public async Task BasicEvent()
        {
            var address = "tcp://localhost:6002";

            var publisherSocket = new PublisherSocket();

            publisherSocket.Bind(address);

            var subscriberSocket = new SubscriberSocket();

            subscriberSocket.Connect(address);

            var router = NetmqMessageRouter
                         .WithPubSubConnecton(publisherSocket, subscriberSocket)
                         .RegisterJsonSerializer()
                         .RegisterEventRoute("EventSource")
                         .RegisterRoute("EventReply", typeof(double));

            router
            .Subscribe("EventSource")
            .WithResponse("EventReply")
            .WithHandler(() => 15d);

            double?answer = null;

            router
            .Subscribe("EventReply")
            .WithHandler((double x) =>
                         answer = x);

            router.OnException += exp =>
            {
            };

            router.StartRouting();

            router
            .SendEvent()
            .To("EventSource");

            await Task.Delay(TimeSpan.FromSeconds(5));

            router
            .StopRouting()
            .Disconnect();

            Assert.AreEqual(15d, answer);
        }
        public async Task RoutingTestForClassSubscriber()
        {
            var address = "tcp://localhost:6000";

            var publisherSocket = new PublisherSocket();

            publisherSocket.Bind(address);

            var subscriberSocket = new SubscriberSocket();

            subscriberSocket.Connect(address);

            var subscriber = new ExampleSubscriber();

            var router = NetmqMessageRouter
                         .WithPubSubConnecton(publisherSocket, subscriberSocket)
                         .WithWorkerPool(numberOfSerializationWorkes: 2, numberOfHandlingWorkes: 8)
                         .RegisterTypeSerializer(new RawDataTypeSerializer())
                         .RegisterTypeSerializer(new BasicTextTypeSerializer())
                         .RegisterGeneralSerializer(new JsonObjectSerializer())
                         .RegisterRoute("TestRoute", typeof(CustomPayload))
                         .RegisterSubscriber(subscriber)
                         .StartRouting();

            router.SendMessage("TestRoute", new CustomPayload("Hellow world", 123));

            router.OnException += exception =>
            {
                // handle any exception
            };

            await Task.Delay(TimeSpan.FromSeconds(5));

            router
            .StopRouting()
            .Disconnect();

            var expectedValue = new CustomPayload("Hellow world", 123);

            Assert.AreEqual(expectedValue, subscriber.PassedValue);
        }