Exemple #1
0
        public static void ChatJoinedHandler(RabbitBus bus)
        {
            var messages = bus.Message <ChatUserJoined>().GetAll();

            Console.WriteLine($"[*System] {messages.Length} users joined in the meantime");

            bus.Message <ChatUserJoined>().Subscribe(wrapper =>
            {
                var payload   = wrapper.Payload;
                var adminText = payload.IsAdmin ? "*" : string.Empty;
                Console.WriteLine($"[{adminText}{payload.Name}] joined");

                return(Task.FromResult(HandleResult.Ok));
            }, SubscriptionType.SharedBetweenConsumers);
        }
Exemple #2
0
        static void Main(string[] args)
        {
            Console.WriteLine("  ###############################");
            Console.WriteLine("  #  RabbitMQ Client 2 (user)   #");
            Console.WriteLine("  ###############################");
            Console.WriteLine();

            var config = RabbitValues.CONFIG;

            config.ConsumerName = "user";

            var random = new Random(DateTime.Now.Millisecond);
            var userId = random.Next(2, 99);

            var user = new ChatUserPayload()
            {
                Id      = userId,
                IsAdmin = false,
                Name    = "User " + userId
            };

            Console.WriteLine($"Logged in as {user.Name}");
            Console.WriteLine();

            using (var bus = new RabbitBus(NullLogger <IBus> .Instance, new OptionsWrapper <BusConfig>(config)))
            {
                bus.Init();

                RabbitValues.ChatHandler(bus);

                bus.Message <ChatUserJoined>().Publish(user);

                while (true)
                {
                    var msg = Console.ReadLine();
                    RabbitValues.DeletePrevConsoleLine();

                    bus.Message <ChatMessageInserted>().Publish(new ChatMessagePayload()
                    {
                        User        = user,
                        IsProtected = msg == "secret",
                        Plaintext   = msg
                    });
                }
            }
        }
Exemple #3
0
        static void Main(string[] args)
        {
            Console.WriteLine("  ###############################");
            Console.WriteLine("  #  RabbitMQ Client 1 (admin)  #");
            Console.WriteLine("  ###############################");
            Console.WriteLine();

            var config = RabbitValues.CONFIG;

            config.ConsumerName = "admin";

            var user = new ChatUserPayload()
            {
                Id      = 1,
                IsAdmin = true,
                Name    = "System Admin"
            };

            Console.WriteLine($"Logged in as {user.Name}");
            Console.WriteLine();

            using (var bus = new RabbitBus(NullLogger <IBus> .Instance, new OptionsWrapper <BusConfig>(config)))
            {
                bus.Init();

                RabbitValues.ChatJoinedHandler(bus);
                RabbitValues.ChatHandler(bus);

                bus.Message <ChatUserJoined>().Publish(user);

                while (true)
                {
                    var msg = Console.ReadLine();
                    RabbitValues.DeletePrevConsoleLine();

                    bus.Message <ChatMessageInserted>().Publish(new ChatMessagePayload()
                    {
                        User        = user,
                        IsProtected = msg == "secret",
                        Plaintext   = msg
                    });
                }
            }
        }
Exemple #4
0
        public static void ChatHandler(RabbitBus bus)
        {
            bus.Message <ChatMessageInserted>().Subscribe(wrapper =>
            {
                var payload = wrapper.Payload;
                if (payload.IsProtected)
                {
                    Console.WriteLine($"[{payload.User.Name}] > *** protected ***");
                }
                else
                {
                    Console.WriteLine($"[{payload.User.Name}] > {payload.Plaintext}");
                }

                return(Task.FromResult(HandleResult.Ok));
            }, SubscriptionType.PerConsumer);
        }