本文整理汇总了C#中ZSocket.SubscribeAll方法的典型用法代码示例。如果您正苦于以下问题:C# ZSocket.SubscribeAll方法的具体用法?C# ZSocket.SubscribeAll怎么用?C# ZSocket.SubscribeAll使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ZSocket
的用法示例。
在下文中一共展示了ZSocket.SubscribeAll方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Main
public static void Main(string[] args)
{
//
// Synchronized subscriber
//
// Author: metadings
//
using (var context = new ZContext())
using (var subscriber = new ZSocket(context, ZSocketType.SUB))
using (var syncclient = new ZSocket(context, ZSocketType.REQ))
{
// First, connect our subscriber socket
subscriber.Connect("tcp://127.0.0.1:5561");
subscriber.SubscribeAll();
// 0MQ is so fast, we need to wait a while…
Thread.Sleep(1000);
// Second, synchronize with publisher
syncclient.Connect("tcp://127.0.0.1:5562");
// - send a synchronization request
syncclient.Send(new ZFrame());
// - wait for synchronization reply
syncclient.ReceiveFrame();
// Third, get our updates and report how many we got
int i = 0;
while (true)
{
using (ZFrame frame = subscriber.ReceiveFrame())
{
string text = frame.ReadString();
if (text == "END")
{
break;
}
frame.Position = 0;
Console.WriteLine("Receiving {0}…", frame.ReadInt32());
++i;
}
}
Console.WriteLine("Received {0} updates.", i);
Console.ReadLine();
}
}
示例2: TaskWork2
public static void TaskWork2(string[] args)
{
//
// Task worker - design 2
// Adds pub-sub flow to receive and respond to kill signal
//
// Author: metadings
//
// Socket to receive messages on,
// Socket to send messages to and
// Socket for control input
using (var context = new ZContext())
using (var receiver = new ZSocket(context, ZSocketType.PULL))
using (var sender = new ZSocket(context, ZSocketType.PUSH))
using (var controller = new ZSocket(context, ZSocketType.SUB))
{
receiver.Connect("tcp://127.0.0.1:5557");
sender.Connect("tcp://127.0.0.1:5558");
controller.Connect("tcp://127.0.0.1:5559");
controller.SubscribeAll();
var poll = ZPollItem.CreateReceiver();
ZError error;
ZMessage message;
while (true)
{
// Process messages from either socket
if (receiver.PollIn(poll, out message, out error, TimeSpan.FromMilliseconds(64)))
{
int workload = message[0].ReadInt32();
Console.WriteLine("{0}.", workload); // Show progress
Thread.Sleep(workload); // Do the work
sender.Send(new byte[0], 0, 0); // Send results to sink
}
// Any waiting controller command acts as 'KILL'
if (controller.PollIn(poll, out message, out error, TimeSpan.FromMilliseconds(64)))
{
break; // Exit loop
}
}
}
}
示例3: SuiSnail_Subscriber
static void SuiSnail_Subscriber(ZContext context, ZSocket backend, CancellationTokenSource cancellor, object[] args)
{
// This is our subscriber. It connects to the publisher and subscribes
// to everything. It sleeps for a short time between messages to
// simulate doing too much work. If a message is more than one second
// late, it croaks.
using (var subscriber = new ZSocket(context, ZSocketType.SUB))
{
// Subscribe to everything
subscriber.SubscribeAll();
subscriber.Connect("tcp://127.0.0.1:5556");
ZFrame incoming;
ZError error;
var rnd = new Random();
while (!cancellor.IsCancellationRequested)
{
// Get and process messages
if (null != (incoming = subscriber.ReceiveFrame(out error)))
{
string terms = incoming.ReadString();
Console.WriteLine(terms);
var clock = DateTime.Parse(terms);
// Suicide snail logic
if (DateTime.UtcNow - clock > SuiSnail_MAX_ALLOWED_DELAY)
{
Console.WriteLine("E: subscriber cannot keep up, aborting");
break;
}
// Work for 1 msec plus some random additional time
Thread.Sleep(1 + rnd.Next(200));
}
else
{
if (error == ZError.ETERM)
break; // Interrupted
throw new ZException(error);
}
}
backend.Send(new ZFrame("gone and died"));
}
}
示例4: Peering1
//
// Broker peering simulation (part 1)
// Prototypes the state flow
//
// Author: metadings
//
public static void Peering1(string[] args)
{
// First argument is this broker's name
// Other arguments are our peers' names
//
if (args == null || args.Length < 2)
{
Console.WriteLine();
Console.WriteLine("Usage: {0} Peering1 World Receiver0", AppDomain.CurrentDomain.FriendlyName);
Console.WriteLine(" {0} Peering1 Receiver0 World", AppDomain.CurrentDomain.FriendlyName);
Console.WriteLine();
return;
}
string self = args[0];
Console.WriteLine("I: preparing broker as {0}", self);
using (var context = new ZContext())
using (var backend = new ZSocket(context, ZSocketType.PUB))
using (var frontend = new ZSocket(context, ZSocketType.SUB))
{
// Bind backend to endpoint
backend.Bind("tcp://127.0.0.1:" + Peering1_GetPort(self));
// Connect frontend to all peers
frontend.SubscribeAll();
for (int i = 1; i < args.Length; ++i)
{
string peer = args[i];
Console.WriteLine("I: connecting to state backend at {0}", peer);
frontend.Connect("tcp://127.0.0.1:" + Peering1_GetPort(peer));
}
// The main loop sends out status messages to peers, and collects
// status messages back from peers. The zmq_poll timeout defines
// our own heartbeat:
ZError error;
ZMessage incoming;
var poll = ZPollItem.CreateReceiver();
var rnd = new Random();
while (true)
{
// Poll for activity, or 1 second timeout
if (!frontend.PollIn(poll, out incoming, out error, TimeSpan.FromSeconds(1)))
{
if (error == ZError.EAGAIN)
{
using (var output = new ZMessage())
{
output.Add(new ZFrame(self));
var outputNumber = ZFrame.Create(4);
outputNumber.Write(rnd.Next(10));
output.Add(outputNumber);
backend.Send(output);
}
continue;
}
if (error == ZError.ETERM)
return;
throw new ZException(error);
}
using (incoming)
{
string peer_name = incoming[0].ReadString();
int available = incoming[1].ReadInt32();
Console.WriteLine("{0} - {1} workers free", peer_name, available);
}
}
}
}
示例5: LVCache
public static void LVCache(string[] args)
{
//
// Last value cache
// Uses XPUB subscription messages to re-send data
//
// Author: metadings
//
using (var context = new ZContext())
using (var frontend = new ZSocket(context, ZSocketType.SUB))
using (var backend = new ZSocket(context, ZSocketType.XPUB))
{
// Subscribe to every single topic from publisher
frontend.Bind("tcp://*:5557");
frontend.SubscribeAll();
backend.Bind("tcp://*:5558");
// Store last instance of each topic in a cache
var cache = new HashSet<LVCacheItem>();
// We route topic updates from frontend to backend, and
// we handle subscriptions by sending whatever we cached,
// if anything:
var p = ZPollItem.CreateReceiver();
ZMessage msg;
ZError error;
while (true)
{
// Any new topic data we cache and then forward
if (frontend.PollIn(p, out msg, out error, TimeSpan.FromMilliseconds(1)))
{
using (msg)
{
string topic = msg[0].ReadString();
string current = msg[1].ReadString();
LVCacheItem previous = cache.FirstOrDefault(item => topic == item.Topic);
if (previous != null)
{
cache.Remove(previous);
}
cache.Add(new LVCacheItem { Topic = topic, Current = current });
backend.Send(msg);
}
}
else
{
if (error == ZError.ETERM)
break; // Interrupted
if (error != ZError.EAGAIN)
throw new ZException(error);
}
// When we get a new subscription, we pull data from the cache:
if (backend.PollIn(p, out msg, out error, TimeSpan.FromMilliseconds(1)))
{
using (msg)
{
// Event is one byte 0=unsub or 1=sub, followed by topic
byte subscribe = msg[0].ReadAsByte();
if (subscribe == 0x01)
{
string topic = msg[0].ReadString();
LVCacheItem previous = cache.FirstOrDefault(item => topic == item.Topic);
if (previous != null)
{
Console.WriteLine("Sending cached topic {0}", topic);
backend.SendMore(new ZFrame(previous.Topic));
backend.Send(new ZFrame(previous.Current));
}
else
{
Console.WriteLine("Failed to send cached topic {0}!", topic);
}
}
}
}
else
{
if (error == ZError.ETERM)
break; // Interrupted
if (error != ZError.EAGAIN)
throw new ZException(error);
}
}
}
}