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


C# RouterSocket.ReceiveMessage方法代码示例

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


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

示例1: Router_Dealer_Demonstrating_Messages_From_Subscribers_To_Publisher

		public void Router_Dealer_Demonstrating_Messages_From_Subscribers_To_Publisher()
		{
			// NOTES
			// 1. Use ThreadLocal<DealerSocket> where each thread has
			//    its own client DealerSocket to talk to server
			// 2. Each thread can send using it own socket
			// 3. Each thread socket is added to poller

			const int delay = 500; // millis

			var clientSocketPerThread = new ThreadLocal<DealerSocket>();

			using (var server = new RouterSocket("@tcp://127.0.0.1:5556"))
			{
				using (var poller = new NetMQPoller())
				{
					// Start some threads, each with its own DealerSocket
					// to talk to the server socket. Creates lots of sockets,
					// but no nasty race conditions no shared state, each
					// thread has its own socket, happy days.
					for (int i = 0; i < 4; i++)
					{
						Task.Factory.StartNew(state =>
						{
							DealerSocket client = null;

							if (!clientSocketPerThread.IsValueCreated)
							{
								client = new DealerSocket();
								client.Options.Identity =
									Encoding.Unicode.GetBytes(state.ToString());
								client.Connect("tcp://127.0.0.1:5556");
								client.ReceiveReady += Client_ReceiveReady;
								clientSocketPerThread.Value = client;
								poller.Add(client);
							}
							else
							{
								client = clientSocketPerThread.Value;
							}

							while (true)
							{
								NetMQMessage messageToServer = new NetMQMessage();
								messageToServer.AppendEmptyFrame();
								messageToServer.Append(state.ToString());
								Console.WriteLine("======================================");
								Console.WriteLine(" OUTGOING MESSAGE TO SERVER ");
								Console.WriteLine("======================================");
								PrintFrames("Client Sending", messageToServer);
								client.SendMultipartMessage(messageToServer);
								Thread.Sleep(delay);
							}

						},
							string.Format("client {0}", i),
							TaskCreationOptions.LongRunning);
					}

					// start the poller
					poller.RunAsync();

					// server loop
					for(int i=0;i<6;i++)
					{
						NetMQMessage clientMessage = server.ReceiveMessage();
						Console.WriteLine("======================================");
						Console.WriteLine(" INCOMING CLIENT MESSAGE FROM CLIENT ");
						Console.WriteLine("======================================");
						PrintFrames("Server receiving", clientMessage);
						if (clientMessage.FrameCount == 3)
						{
							var clientAddress = clientMessage[0];
							var clientOriginalMessage = clientMessage[2].ConvertToString();
							string response = string.Format("{0} back from server {1}",
								clientOriginalMessage,
								DateTime.Now.ToLongTimeString());
							var messageToClient = new NetMQMessage();
							messageToClient.Append(clientAddress);
							messageToClient.AppendEmptyFrame();
							messageToClient.Append(response);
							server.SendMultipartMessage(messageToClient);
						}
					}
				}
			}
		}
开发者ID:dalebrubaker,项目名称:NetMQ.ReactiveExtensions,代码行数:87,代码来源:RouterDealerTest1.cs


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