当前位置: 首页>>代码示例>>C#>>正文


C# RouterSocket.ReceiveFrameString方法代码示例

本文整理汇总了C#中NetMQ.Sockets.RouterSocket.ReceiveFrameString方法的典型用法代码示例。如果您正苦于以下问题:C# RouterSocket.ReceiveFrameString方法的具体用法?C# RouterSocket.ReceiveFrameString怎么用?C# RouterSocket.ReceiveFrameString使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在NetMQ.Sockets.RouterSocket的用法示例。


在下文中一共展示了RouterSocket.ReceiveFrameString方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Mandatory

        public void Mandatory()
        {
            using (var router = new RouterSocket())
            {
                router.Options.RouterMandatory = true;
                router.Bind("tcp://127.0.0.1:5555");

                using (var dealer = new DealerSocket())
                {
                    dealer.Options.Identity = Encoding.ASCII.GetBytes("1");
                    dealer.Connect("tcp://127.0.0.1:5555");

                    dealer.SendFrame("Hello");

                    Assert.AreEqual("1", router.ReceiveFrameString());
                    Assert.AreEqual("Hello", router.ReceiveFrameString());
                }

                Thread.Sleep(100);

                Assert.Throws<HostUnreachableException>(() => router.SendMoreFrame("1").SendFrame("Hello"));
            }
        }
开发者ID:somdoron,项目名称:netmq,代码行数:23,代码来源:RouterTests.cs

示例2: TwoMessagesFromRouterToDealer

        public void TwoMessagesFromRouterToDealer()
        {
            using (var server = new RouterSocket())
            using (var client = new DealerSocket())
            using (var poller = new NetMQPoller { client })
            {
                var port = server.BindRandomPort("tcp://*");
                client.Connect("tcp://127.0.0.1:" + port);
                var cnt = 0;
                client.ReceiveReady += (sender, e) =>
                {
                    var strs = e.Socket.ReceiveMultipartStrings();
                    foreach (var str in strs)
                    {
                        Console.WriteLine(str);
                    }
                    cnt++;
                    if (cnt == 2)
                    {
                        poller.Stop();
                    }
                };
                byte[] clientId = Encoding.Unicode.GetBytes("ClientId");
                client.Options.Identity = clientId;

                const string request = "GET /\r\n";

                const string response = "HTTP/1.0 200 OK\r\n" +
                        "Content-Type: text/plain\r\n" +
                        "\r\n" +
                        "Hello, World!";

                client.SendFrame(request);

                byte[] serverId = server.ReceiveFrameBytes();
                Assert.AreEqual(request, server.ReceiveFrameString());

                // two messages in a row, not frames
                server.SendMoreFrame(serverId).SendFrame(response);
                server.SendMoreFrame(serverId).SendFrame(response);

                poller.Run();
            }
        }
开发者ID:NetMQ,项目名称:NetMQ3-x,代码行数:44,代码来源:RouterTests.cs

示例3: Handover

        public void Handover()
        {
            using (var router = new RouterSocket())
            using (var dealer1 = new DealerSocket())
            {
                router.Options.RouterHandover = true;
                router.Bind("inproc://127.0.0.1:5555");
                dealer1.Options.Identity = Encoding.ASCII.GetBytes("ID");
                dealer1.Connect("inproc://127.0.0.1:5555");
                dealer1.SendMoreFrame("Hello").SendFrame("World");

                var identity = router.ReceiveFrameString();
                Assert.AreEqual("ID", identity);

                using (var dealer2 = new DealerSocket())
                {
                    dealer2.Options.Identity = Encoding.ASCII.GetBytes("ID");
                    dealer2.Connect("inproc://127.0.0.1:5555");

                    // We have new peer which should take over, however we are still reading a message
                    var message = router.ReceiveFrameString();
                    Assert.AreEqual("Hello", message);
                    message = router.ReceiveFrameString();
                    Assert.AreEqual("World", message);

                    dealer2.SendMoreFrame("Hello").SendFrame("World");
                    identity = router.ReceiveFrameString();
                    Assert.AreEqual("ID", identity);

                    message = router.ReceiveFrameString();
                    Assert.AreEqual("Hello", message);

                    message = router.ReceiveFrameString();
                    Assert.AreEqual("World", message);
                }
            }
        }
开发者ID:somdoron,项目名称:netmq,代码行数:37,代码来源:RouterTests.cs

示例4: HasInTest

        public void HasInTest()
        {
            using (var server = new RouterSocket())
            using (var client = new DealerSocket())
            {
                var port = server.BindRandomPort("tcp://*");

                // no one sent a message so it should be false
                Assert.IsFalse(server.HasIn);

                client.Connect("tcp://localhost:" + port);

                // wait for the client to connect
                Thread.Sleep(100);

                // now we have one client connected but didn't send a message yet
                Assert.IsFalse(server.HasIn);

                client.SendFrame("1");

                // wait for the message to arrive
                Thread.Sleep(100);

                // the has in should indicate a message is ready
                Assert.IsTrue(server.HasIn);

                server.SkipFrame(); // identity
                string message = server.ReceiveFrameString();

                Assert.AreEqual(message, "1");

                // we read the message, it should false again
                Assert.IsFalse(server.HasIn);
            }
        }
开发者ID:hdxhan,项目名称:netmq,代码行数:35,代码来源:SocketTests.cs

示例5: AddTwoSocketAfterRemoving

        public void AddTwoSocketAfterRemoving()
        {
            using (var router1 = new RouterSocket())
            using (var router2 = new RouterSocket())
            using (var router3 = new RouterSocket())
            using (var router4 = new RouterSocket())
            using (var dealer1 = new DealerSocket())
            using (var dealer2 = new DealerSocket())
            using (var dealer3 = new DealerSocket())
            using (var dealer4 = new DealerSocket())
            using (var poller = new NetMQPoller { router1, router2 })
            {
                int port1 = router1.BindRandomPort("tcp://127.0.0.1");
                int port2 = router2.BindRandomPort("tcp://127.0.0.1");
                int port3 = router3.BindRandomPort("tcp://127.0.0.1");
                int port4 = router4.BindRandomPort("tcp://127.0.0.1");

                dealer1.Connect("tcp://127.0.0.1:" + port1);
                dealer2.Connect("tcp://127.0.0.1:" + port2);
                dealer3.Connect("tcp://127.0.0.1:" + port3);
                dealer4.Connect("tcp://127.0.0.1:" + port4);

                int router1Arrived = 0;
                int router2Arrived = 0;
                bool router3Arrived = false;
                bool router4Arrived = false;

                var signal1 = new ManualResetEvent(false);
                var signal2 = new ManualResetEvent(false);
                var signal3 = new ManualResetEvent(false);
                var signal4 = new ManualResetEvent(false);

                router1.ReceiveReady += (s, e) =>
                {
                    router1Arrived++;
                    router1.SkipFrame(); // identity
                    router1.SkipFrame(); // message
                    poller.Remove(router1);
                    signal1.Set();
                };

                router2.ReceiveReady += (s, e) =>
                {
                    router2Arrived++;
                    router2.SkipFrame(); // identity
                    router2.SkipFrame(); // message

                    if (router2Arrived == 1)
                    {
                        poller.Add(router3);
                        poller.Add(router4);
                        signal2.Set();
                    }
                };

                router3.ReceiveReady += (s, e) =>
                {
                    router3.SkipFrame(); // identity
                    router3.SkipFrame(); // message
                    router3Arrived = true;
                    signal3.Set();
                };

                router4.ReceiveReady += (s, e) =>
                {
                    router4.SkipFrame(); // identity
                    router4.SkipFrame(); // message
                    router4Arrived = true;
                    signal4.Set();
                };

                poller.RunAsync();

                dealer1.SendFrame("1");
                Assert.IsTrue(signal1.WaitOne(300));
                dealer2.SendFrame("2");
                Assert.IsTrue(signal2.WaitOne(300));
                dealer3.SendFrame("3");
                dealer4.SendFrame("4");
                dealer2.SendFrame("2");
                dealer1.SendFrame("1");
                Assert.IsTrue(signal3.WaitOne(300));
                Assert.IsTrue(signal4.WaitOne(300));

                poller.Stop();

                router1.SkipFrame();
                bool more;
                Assert.AreEqual("1", router1.ReceiveFrameString(out more));
                Assert.IsFalse(more);

                Assert.AreEqual(1, router1Arrived);
                Assert.AreEqual(2, router2Arrived);
                Assert.IsTrue(router3Arrived);
                Assert.IsTrue(router4Arrived);
            }
        }
开发者ID:cjkao,项目名称:netmq,代码行数:97,代码来源:NetMQPollerTest.cs

示例6: Messages_From_Router_To_Dealer

		public void Messages_From_Router_To_Dealer()
		{
			Console.Write("Test sending message from publisher(router) to subscribers (dealer).\n");

			int maxMessage = 5;
			CountdownEvent cd = new CountdownEvent(maxMessage);

			string endpoint;

			using (var publisher = new RouterSocket()) 
			using (var subscriber = new DealerSocket())
			using (var poller = new NetMQPoller { subscriber })
			{
				publisher.Bind("tcp://127.0.0.1:0");
				endpoint = publisher.Options.LastEndpoint;

				subscriber.Connect(endpoint);
				subscriber.ReceiveReady += (sender, e) =>
				{
					var strs = e.Socket.ReceiveMultipartStrings();
					foreach (var str in strs)
					{
						Console.WriteLine("Subscribe: " + str);
					}
					cd.Signal();
				};
				byte[] clientId = Encoding.Unicode.GetBytes("ClientId");
				subscriber.Options.Identity = clientId;

				const string request = "Ping";

				// Work around "feature" of router/dealer: the publisher does not know the subscriber exists, until it
                // sends at least one message which makes it necessary to open the connection. I believe this is a
                // low-level feature of the TCP/IP transport.
				subscriber.SendFrame(request); // Ping.

				byte[] serverId = publisher.ReceiveFrameBytes();
				Assert.AreEqual(request, publisher.ReceiveFrameString());

				for (int i = 0; i < maxMessage; i++)
				{
					string msg = string.Format("[message: {0}]", i);
					Console.Write("Publish: {0}\n", msg);
					publisher.SendMoreFrame(serverId).SendFrame(msg);
				}

				poller.RunAsync();

				if (cd.Wait(TimeSpan.FromSeconds(10)) == false) // Blocks until _countdown.Signal has been called.
				{
					Assert.Fail("Timed out, this test should complete in less than 10 seconds.");
				}
			}
		}
开发者ID:dalebrubaker,项目名称:NetMQ.ReactiveExtensions,代码行数:54,代码来源:RouterDealerTest2.cs

示例7: Messages_From_Dealer_To_Router

		public void Messages_From_Dealer_To_Router()
		{
			int maxMessage = 5;
			CountdownEvent cd = new CountdownEvent(maxMessage);

			Console.Write("Test sending message from subscribers (dealer) to publisher(router).\n");

			using (var publisher = new RouterSocket())
			using (var subscriber = new DealerSocket())
			using (var poller = new NetMQPoller { subscriber })
			{
				var port = publisher.BindRandomPort("tcp://*");
				subscriber.Connect("tcp://127.0.0.1:" + port);
				subscriber.ReceiveReady += (sender, e) =>
				{
					var strs = e.Socket.ReceiveMultipartStrings();
					foreach (var str in strs)
					{
						Console.WriteLine(str);
					}
					cd.Signal();
				};
				byte[] clientId = Encoding.Unicode.GetBytes("ClientId");
				subscriber.Options.Identity = clientId;

				const string request = "GET /\r\n";

				const string response = "HTTP/1.0 200 OK\r\n" +
						"Content-Type: text/plain\r\n" +
						"\r\n" +
						"Hello, World!";

				subscriber.SendFrame(request);

				byte[] serverId = publisher.ReceiveFrameBytes();
				Assert.AreEqual(request, publisher.ReceiveFrameString());

				for (int i = 0; i < maxMessage; i++)
				{
					publisher.SendMoreFrame(serverId).SendFrame(response);
				}

				poller.RunAsync();

				if (cd.Wait(TimeSpan.FromSeconds(10)) == false) // Blocks until _countdown.Signal has been called.
				{
					Assert.Fail("Timed out, this test should complete in less than 10 seconds.");
				}
			}
		}
开发者ID:dalebrubaker,项目名称:NetMQ.ReactiveExtensions,代码行数:50,代码来源:RouterDealerTest2.cs


注:本文中的NetMQ.Sockets.RouterSocket.ReceiveFrameString方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。