本文整理汇总了C#中NetMQ.Sockets.RouterSocket.SendMultipartMessage方法的典型用法代码示例。如果您正苦于以下问题:C# RouterSocket.SendMultipartMessage方法的具体用法?C# RouterSocket.SendMultipartMessage怎么用?C# RouterSocket.SendMultipartMessage使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类NetMQ.Sockets.RouterSocket
的用法示例。
在下文中一共展示了RouterSocket.SendMultipartMessage方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Router_Dealer_Demonstrating_Messages_From_Publisher_To_Subscribers
public void Router_Dealer_Demonstrating_Messages_From_Publisher_To_Subscribers()
{
// 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>();
string endpoint;
using (var server = new RouterSocket("@tcp://127.0.0.1:0")) // If we specify 0, it will choose a random port for us.
{
endpoint = server.Options.LastEndpoint; // Lets us know which port was chosen.
Console.Write("Last endpoint, including port: {0}\n", server.Options.LastEndpoint);
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(endpoint);
//client.ReceiveReady += Client_ReceiveReady;
clientSocketPerThread.Value = client;
poller.Add(client);
}
else
{
client = clientSocketPerThread.Value;
}
Thread.Sleep(3000); // Wait until server is up.
client.SendFrame("Ping");
while (true)
{
Console.Write("Client {0}: Waiting for ping...\n", i);
// 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.
var clientMessage = client.ReceiveMultipartMessage();
Console.WriteLine("======================================");
Console.WriteLine(" INCOMING CLIENT MESSAGE FROM SERVER");
Console.WriteLine("======================================");
PrintFrames("Server receiving", clientMessage);
}
},
string.Format("client {0}", i),
TaskCreationOptions.LongRunning);
}
// start the poller
poller.RunAsync();
// server loop
int sequenceNo = 0;
for (int i=0;i<10;i++)
{
NetMQMessage messageToServer = new NetMQMessage();
messageToServer.AppendEmptyFrame();
messageToServer.Append(sequenceNo.ToString());
sequenceNo++;
Console.WriteLine("======================================");
Console.WriteLine(" OUTGOING MESSAGE {0} TO CLIENTS ", sequenceNo);
Console.WriteLine("======================================");
PrintFrames("Client Sending", messageToServer);
server.SendMultipartMessage(messageToServer);
Thread.Sleep(delay);
}
Console.WriteLine("Finished.");
}
}
}
示例2: 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);
}
}
}
}
}
示例3: RouterDealerMessaging
public void RouterDealerMessaging()
{
using (var server = new RouterSocket())
using (var client = new DealerSocket())
{
int port = server.BindRandomPort("tcp://127.0.0.1");
client.Connect("tcp://127.0.0.1:" + port);
var clientOutgoingMessage = new NetMQMessage();
clientOutgoingMessage.Append("Hello");
client.SendMultipartMessage(clientOutgoingMessage);
NetMQMessage serverIncomingMessage = server.ReceiveMultipartMessage();
// number of frames should be one because first message should be identity of client
Assert.AreEqual(2, serverIncomingMessage.FrameCount);
Assert.AreEqual("Hello", serverIncomingMessage[1].ConvertToString());
var serverOutgoingMessage = new NetMQMessage();
// first adding the identity
serverOutgoingMessage.Append(serverIncomingMessage[0]);
serverOutgoingMessage.Append("World");
server.SendMultipartMessage(serverOutgoingMessage);
var incomingClientMessage = client.ReceiveMultipartMessage();
Assert.AreEqual(1, incomingClientMessage.FrameCount);
Assert.AreEqual("World", incomingClientMessage[0].ConvertToString());
}
}