Esempio n. 1
0
        public async Task Test3()
        {
            IPAddress   iPAddress = (await Dns.GetHostAddressesAsync("127.0.0.1"))[0];
            TcpListener server    = new TcpListener(iPAddress, 8414);

            server.Start();
            var service = new Broker(new Service()).Service;

            ServiceCodec.Instance.Debug = true;
            service.Use(Log.IOHandler)
            .Use(Log.InvokeHandler)
            .Bind(server);

            var client1   = new Client("tcp4://127.0.0.1:8414");
            var prosumer1 = new Prosumer(client1, "1")
            {
                OnSubscribe = (topic) => {
                    Console.WriteLine(topic + " is subscribed.");
                },
                OnUnsubscribe = (topic) => {
                    Console.WriteLine(topic + " is unsubscribed.");
                }
            };
            var client2   = new Client("tcp4://127.0.0.1:8414");
            var prosumer2 = new Prosumer(client2, "2");
            await prosumer1.Subscribe <string>("test", (data, from) => {
                System.Console.WriteLine(data);
                Assert.AreEqual("hello", data);
                System.Console.WriteLine(from);
                Assert.AreEqual("2", from);
            });

            await prosumer1.Subscribe <string>("test2", (data) => {
                System.Console.WriteLine(data);
                Assert.AreEqual("world", data);
            });

            await prosumer1.Subscribe <Exception>("test3", (data) => {
                System.Console.WriteLine(data);
                Assert.AreEqual("error", data.Message);
            });

            var r1 = prosumer2.Push("hello", "test", "1");
            var r2 = prosumer2.Push("hello", "test", "1");
            var r3 = prosumer2.Push("world", "test2", "1");
            var r4 = prosumer2.Push("world", "test2", "1");
            var r5 = prosumer2.Push(new Exception("error"), "test3", "1");
            var r6 = prosumer2.Push(new Exception("error"), "test3", "1");
            await Task.WhenAll(r1, r2, r3, r4, r5, r6);

            await Task.Delay(10);

            await prosumer1.Unsubscribe("test");

            await prosumer1.Unsubscribe("test2");

            await prosumer1.Unsubscribe("test3");

            server.Stop();
        }
Esempio n. 2
0
        public async Task Test3()
        {
            UdpClient server  = new UdpClient(8414);
            var       service = new Broker(new Service()).Service;

            ServiceCodec.Instance.Debug = true;
            service.Use(Log.IOHandler)
            .Use(Log.InvokeHandler)
            .Bind(server);

            var client1   = new Client("udp://127.0.0.1:8414");
            var prosumer1 = new Prosumer(client1, "1");

            prosumer1.OnSubscribe += (topic) => {
                Console.WriteLine(topic + " is subscribed.");
            };
            prosumer1.OnUnsubscribe += (topic) => {
                Console.WriteLine(topic + " is unsubscribed.");
            };
            var client2   = new Client("udp://127.0.0.1:8414");
            var prosumer2 = new Prosumer(client2, "2");
            await prosumer1.Subscribe <string>("test", (data, from) => {
                System.Console.WriteLine(data);
                Assert.AreEqual("hello", data);
                System.Console.WriteLine(from);
                Assert.AreEqual("2", from);
            }).ConfigureAwait(false);

            await prosumer1.Subscribe <string>("test2", (data) => {
                System.Console.WriteLine(data);
                Assert.AreEqual("world", data);
            }).ConfigureAwait(false);

            await prosumer1.Subscribe <Exception>("test3", (data) => {
                System.Console.WriteLine(data);
                Assert.AreEqual("error", data.Message);
            }).ConfigureAwait(false);

            var r1 = prosumer2.Push("hello", "test", "1");
            var r2 = prosumer2.Push("hello", "test", "1");
            var r3 = prosumer2.Push("world", "test2", "1");
            var r4 = prosumer2.Push("world", "test2", "1");
            var r5 = prosumer2.Push(new Exception("error"), "test3", "1");
            var r6 = prosumer2.Push(new Exception("error"), "test3", "1");
            await Task.WhenAll(r1, r2, r3, r4, r5, r6).ConfigureAwait(false);

            await Task.Delay(10).ConfigureAwait(false);

            await prosumer1.Unsubscribe("test").ConfigureAwait(false);

            await prosumer1.Unsubscribe("test2").ConfigureAwait(false);

            await prosumer1.Unsubscribe("test3").ConfigureAwait(false);

            server.Close();
        }
Esempio n. 3
0
        public async Task Test3()
        {
            HttpListener server = new HttpListener();

            server.Prefixes.Add("http://127.0.0.1:8082/");
            server.Start();
            var service = new Broker(new Service()).Service;

            ServiceCodec.Instance.Debug = true;
            service.Use(Log.IOHandler)
            .Use(Log.InvokeHandler)
            .Bind(server);

            var client1   = new Client("ws://127.0.0.1:8082/");
            var prosumer1 = new Prosumer(client1, "1")
            {
                OnSubscribe = (topic) => {
                    Console.WriteLine(topic + " is subscribed.");
                },
                OnUnsubscribe = (topic) => {
                    Console.WriteLine(topic + " is unsubscribed.");
                }
            };
            var client2   = new Client("ws://127.0.0.1:8082/");
            var prosumer2 = new Prosumer(client2, "2");
            await prosumer1.Subscribe <string>("test", (data, from) => {
                Assert.AreEqual("hello", data);
                Assert.AreEqual("2", from);
            });

            await prosumer1.Subscribe <string>("test2", (data) => {
                Assert.AreEqual("world", data);
            });

            var r1 = prosumer2.Push("hello", "test", "1");
            var r2 = prosumer2.Push("hello", "test", "1");
            var r3 = prosumer2.Push("world", "test2", "1");
            var r4 = prosumer2.Push("world", "test2", "1");
            await Task.WhenAll(r1, r2, r3, r4);

            await Task.Delay(10);

            await prosumer1.Unsubscribe("test");

            await prosumer1.Unsubscribe("test2");

            server.Stop();
        }
Esempio n. 4
0
        public async Task Test3()
        {
            MockServer server  = new MockServer("test3");
            var        service = new Broker(new Service()).Service;

            ServiceCodec.Instance.Debug = true;
            service.Use(Log.IOHandler)
            .Use(Log.InvokeHandler)
            .Bind(server);
            var client1   = new Client("mock://test3");
            var prosumer1 = new Prosumer(client1, "1");

            prosumer1.OnSubscribe += (topic) => {
                Console.WriteLine(topic + " is subscribed.");
            };
            prosumer1.OnUnsubscribe += (topic) => {
                Console.WriteLine(topic + " is unsubscribed.");
            };
            var client2   = new Client("mock://test3");
            var prosumer2 = new Prosumer(client2, "2");
            await prosumer1.Subscribe <string>("test", (data, from) => {
                Assert.AreEqual("hello", data);
                Assert.AreEqual("2", from);
            }).ConfigureAwait(false);

            await prosumer1.Subscribe <string>("test2", (data) => {
                Assert.AreEqual("world", data);
            }).ConfigureAwait(false);

            var r1 = prosumer2.Push("hello", "test", "1");
            var r2 = prosumer2.Push("hello", "test", "1");
            var r3 = prosumer2.Push("world", "test2", "1");
            var r4 = prosumer2.Push("world", "test2", "1");
            await Task.WhenAll(r1, r2, r3, r4).ConfigureAwait(false);

            await Task.Delay(10).ConfigureAwait(false);

            await prosumer1.Unsubscribe("test").ConfigureAwait(false);

            await prosumer1.Unsubscribe("test2").ConfigureAwait(false);

            server.Close();
        }