當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。