本文整理汇总了C#中ZSocket.Connect方法的典型用法代码示例。如果您正苦于以下问题:C# ZSocket.Connect方法的具体用法?C# ZSocket.Connect怎么用?C# ZSocket.Connect使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ZSocket
的用法示例。
在下文中一共展示了ZSocket.Connect方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: LBBroker_Client
// Basic request-reply client using REQ socket
static void LBBroker_Client(ZContext context, int i)
{
// Create a socket
using (var client = new ZSocket(context, ZSocketType.REQ))
{
// Set a printable identity
client.IdentityString = "CLIENT" + i;
// Connect
client.Connect("inproc://frontend");
using (var request = new ZMessage())
{
request.Add(new ZFrame("Hello"));
// Send request
client.Send(request);
}
// Receive reply
using (ZMessage reply = client.ReceiveMessage())
{
Console.WriteLine("CLIENT{0}: {1}", i, reply[0].ReadString());
}
}
}
示例2: Handle
private static void Handle(Header header, ZFrame bodyFrame)
{
//TODO - really we'd have a message handler factory:
if (header.BodyType == typeof(SendFulfilmentCommand).Name)
{
var command = JsonConvert.DeserializeObject<SendFulfilmentCommand>(bodyFrame.ReadString());
var client = FulfilmentClientFactory.GetApiClient(command.FulfilmentType);
try
{
client.Send(command.Address);
Console.WriteLine("*** Sent fulfilment, type: {0}, to address: {1}", command.FulfilmentType, command.Address);
}
catch (Exception ex)
{
Console.WriteLine("*** Fulfilment failed, resending message");
var queueAddress = Config.Get("Queues.Fulfilment.Address");
header.HandledCount++;
header.LastExceptionMessage = ex.Message;
var messageFrames = new List<ZFrame>();
messageFrames.Add(new ZFrame(JsonConvert.SerializeObject(header)));
messageFrames.Add(bodyFrame);
using (var context = new ZContext())
using (var sender = new ZSocket(context, ZSocketType.PUSH))
{
sender.Connect(queueAddress);
sender.Send(new ZMessage(messageFrames));
}
}
}
}
示例3: PSEnvSub
public static void PSEnvSub(string[] args)
{
//
// Pubsub envelope subscriber
//
// Author: metadings
//
// Prepare our context and subscriber
using (var context = new ZContext())
using (var subscriber = new ZSocket(context, ZSocketType.SUB))
{
subscriber.Connect("tcp://127.0.0.1:5563");
subscriber.Subscribe("B");
int subscribed = 0;
while (true)
{
using (ZMessage message = subscriber.ReceiveMessage())
{
subscribed++;
// Read envelope with address
string address = message[0].ReadString();
// Read message contents
string contents = message[1].ReadString();
Console.WriteLine("{0}. [{1}] {2}", subscribed, address, contents);
}
}
}
}
示例4: RTReq_Worker
static void RTReq_Worker(int i)
{
using (var context = new ZContext())
using (var worker = new ZSocket(context, ZSocketType.REQ))
{
worker.IdentityString = "PEER" + i; // Set a printable identity
worker.Connect("tcp://127.0.0.1:5671");
int total = 0;
while (true)
{
// Tell the broker we're ready for work
worker.Send(new ZFrame("Hi Boss"));
// Get workload from broker, until finished
using (ZFrame frame = worker.ReceiveFrame())
{
bool finished = (frame.ReadString() == "Fired!");
if (finished)
{
break;
}
}
total++;
// Do some random work
Thread.Sleep(1);
}
Console.WriteLine("Completed: PEER{0}, {1} tasks", i, total);
}
}
示例5: Espresso
public static void Espresso(string[] args)
{
//
// Espresso Pattern
// This shows how to capture data using a pub-sub proxy
//
// Author: metadings
//
using (var context = new ZContext())
using (var subscriber = new ZSocket(context, ZSocketType.XSUB))
using (var publisher = new ZSocket(context, ZSocketType.XPUB))
using (var listener = new ZSocket(context, ZSocketType.PAIR))
{
new Thread(() => Espresso_Publisher(context)).Start();
new Thread(() => Espresso_Subscriber(context)).Start();
new Thread(() => Espresso_Listener(context)).Start();
subscriber.Connect("tcp://127.0.0.1:6000");
publisher.Bind("tcp://*:6001");
listener.Bind("inproc://listener");
ZError error;
if (!ZContext.Proxy(subscriber, publisher, listener, out error))
{
if (error == ZError.ETERM)
return; // Interrupted
throw new ZException(error);
}
}
}
示例6: Espresso_Subscriber
static void Espresso_Subscriber(ZContext context)
{
// The subscriber thread requests messages starting with
// A and B, then reads and counts incoming messages.
using (var subscriber = new ZSocket(context, ZSocketType.SUB))
{
subscriber.Connect("tcp://127.0.0.1:6001");
subscriber.Subscribe("A");
subscriber.Subscribe("B");
ZError error;
int count = 0;
while (count < 5)
{
var bytes = new byte[10];
int bytesLength;
if (-1 == (bytesLength = subscriber.ReceiveBytes(bytes, 0, bytes.Length, ZSocketFlags.None, out error)))
{
if (error == ZError.ETERM)
return; // Interrupted
throw new ZException(error);
}
++count;
}
Console.WriteLine("I: subscriber counted {0}", count);
}
}
示例7: TaskWorker
public TaskWorker(string workerid="*",string SenderIP = "127.0.0.1", int SenderPort = 5557, string sinkIP = "127.0.0.1", int sinkPort=5558)
{
//
// Task worker
// Connects PULL socket to tcp://localhost:5557
// Collects workloads from ventilator via that socket
// Connects PUSH socket to tcp://localhost:5558
// Sends results to sink via that socket
//
// Author: metadings
//
// Socket to receive messages on and
// Socket to send messages to
using (var context = new ZContext())
using (var receiver = new ZSocket(context, ZSocketType.PULL))
using (var sink = new ZSocket(context, ZSocketType.PUSH))
{
receiver.Connect(String.Format ("tcp://{0}:{1}",SenderIP,SenderPort));
sink.Connect(string.Format("tcp://{0}:{1}",sinkIP,sinkPort ));
Console.WriteLine("Worker " + workerid + " ready.");
// Process tasks forever
while (true)
{
var replyBytes = new byte[4];
receiver.ReceiveBytes(replyBytes, 0, replyBytes.Length);
int workload = BitConverter.ToInt32(replyBytes, 0);
Console.WriteLine("{0}.", workload); // Show progress
Thread.Sleep(workload); // Do the work
sink.Send(new byte[0], 0, 0); // Send results to sink
}
}
}
示例8: TaskWork
public static void TaskWork(string[] args)
{
//
// Task worker
// Connects PULL socket to tcp://127.0.0.1:5557
// Collects workloads from ventilator via that socket
// Connects PUSH socket to tcp://127.0.0.1:5558
// Sends results to sink via that socket
//
// Author: metadings
//
// Socket to receive messages on and
// Socket to send messages to
using (var context = new ZContext())
using (var receiver = new ZSocket(context, ZSocketType.PULL))
using (var sink = new ZSocket(context, ZSocketType.PUSH))
{
receiver.Connect("tcp://127.0.0.1:5557");
sink.Connect("tcp://127.0.0.1:5558");
// Process tasks forever
while (true)
{
var replyBytes = new byte[4];
receiver.ReceiveBytes(replyBytes, 0, replyBytes.Length);
int workload = BitConverter.ToInt32(replyBytes, 0);
Console.WriteLine("{0}.", workload); // Show progress
Thread.Sleep(workload); // Do the work
sink.Send(new byte[0], 0, 0); // Send results to sink
}
}
}
示例9: Espresso_Subscriber
static void Espresso_Subscriber(ZContext context)
{
// The subscriber thread requests messages starting with
// A and B, then reads and counts incoming messages.
using (var subscriber = new ZSocket(context, ZSocketType.SUB))
{
subscriber.Connect("tcp://127.0.0.1:6001");
subscriber.Subscribe("A");
subscriber.Subscribe("B");
ZError error;
ZFrame frame;
int count = 0;
while (count < 5)
{
if (null == (frame = subscriber.ReceiveFrame(out error)))
{
if (error == ZError.ETERM)
return; // Interrupted
throw new ZException(error);
}
++count;
}
Console.WriteLine("I: subscriber counted {0}", count);
}
}
示例10: Espresso0_Listener
static void Espresso0_Listener(ZContext context)
{
// The listener receives all messages flowing through the proxy, on its
// pipe. In CZMQ, the pipe is a pair of ZMQ_PAIR sockets that connect
// attached child threads. In other languages your mileage may vary:
using (var listener = new ZSocket(context, ZSocketType.PAIR))
{
listener.Connect("inproc://listener");
//Print everything that arrives on pipe
ZError error;
ZFrame frame;
while (true)
{
if (null == (frame = listener.ReceiveFrame(out error)))
{
if (error == ZError.ETERM)
return; // Interrupted
throw new ZException(error);
}
using (frame)
frame.DumpZfrm();
}
}
}
示例11: RRClient
public static void RRClient(string[] args)
{
//
// Hello World client
// Connects REQ socket to tcp://localhost:5559
// Sends "Hello" to server, expects "World" back
//
// Author: metadings
//
// Socket to talk to server
using (var context = new ZContext())
using (var requester = new ZSocket(context, ZSocketType.REQ))
{
requester.Connect("tcp://127.0.0.1:5559");
for (int n = 0; n < 10; ++n)
{
requester.Send(new ZFrame("Hello"));
using (ZFrame reply = requester.ReceiveFrame())
{
Console.WriteLine("Hello {0}!", reply.ReadString());
}
}
}
}
示例12: Espresso0_Subscriber
// The subscriber thread requests messages starting with
// A and B, then reads and counts incoming messages.
static void Espresso0_Subscriber(ZContext context)
{
// Subscrie to "A" and "B"
using (var subscriber = new ZSocket(context, ZSocketType.SUB))
{
subscriber.Connect("tcp://127.0.0.1:6001");
subscriber.Subscribe("A");
subscriber.Subscribe("B");
ZError error;
ZFrame frm;
int count = 0;
while (count < 5)
{
if (null == (frm = subscriber.ReceiveFrame(out error)))
{
if (error == ZError.ETERM)
return; // Interrupted
throw new ZException(error);
}
++count;
}
Console.WriteLine("I: subscriber counted {0}", count);
}
}
示例13: ConnectToBroker
public void ConnectToBroker()
{
// Connect or reconnect to broker
Client = new ZSocket(_context, ZSocketType.REQ);
Client.Connect(Broker);
if (Verbose)
"I: connecting to broker at '{0}'...".DumpString(Broker);
}
示例14: Start
public override void Start()
{
base.Start();
if (Frontend == null)
{
Frontend = ZSocket.Create(Context, ZSocketType.PAIR);
Frontend.Connect(Endpoint);
}
}
示例15: LBBroker_Worker
static void LBBroker_Worker(ZContext context, int i)
{
// This is the worker task, using a REQ socket to do load-balancing.
// Create socket
using (var worker = new ZSocket(context, ZSocketType.REQ))
{
// Set a printable identity
worker.IdentityString = "WORKER" + i;
// Connect
worker.Connect("inproc://backend");
// Tell broker we're ready for work
using (var ready = new ZFrame("READY"))
{
worker.Send(ready);
}
ZError error;
ZMessage request;
while (true)
{
// Get request
if (null == (request = worker.ReceiveMessage(out error)))
{
// We are using "out error",
// to NOT throw a ZException ETERM
if (error == ZError.ETERM)
break;
throw new ZException(error);
}
using (request)
{
string worker_id = request[0].ReadString();
string requestText = request[2].ReadString();
Console.WriteLine("WORKER{0}: {1}", i, requestText);
// Send reply
using (var commit = new ZMessage())
{
commit.Add(new ZFrame(worker_id));
commit.Add(new ZFrame());
commit.Add(new ZFrame("OK"));
worker.Send(commit);
}
}
}
}
}