本文整理汇总了C#中NATS.Client.ConnectionFactory.CreateConnection方法的典型用法代码示例。如果您正苦于以下问题:C# ConnectionFactory.CreateConnection方法的具体用法?C# ConnectionFactory.CreateConnection怎么用?C# ConnectionFactory.CreateConnection使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类NATS.Client.ConnectionFactory
的用法示例。
在下文中一共展示了ConnectionFactory.CreateConnection方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: TestServersOption
public void TestServersOption()
{
IConnection c = null;
ConnectionFactory cf = new ConnectionFactory();
Options o = ConnectionFactory.GetDefaultOptions();
o.NoRandomize = true;
UnitTestUtilities.testExpectedException(
() => { cf.CreateConnection(); },
typeof(NATSNoServersException));
o.Servers = testServers;
UnitTestUtilities.testExpectedException(
() => { cf.CreateConnection(o); },
typeof(NATSNoServersException));
// Make sure we can connect to first server if running
using (NATSServer ns = utils.CreateServerOnPort(1222))
{
c = cf.CreateConnection(o);
Assert.IsTrue(testServers[0].Equals(c.ConnectedUrl));
c.Close();
}
// make sure we can connect to a non-first server.
using (NATSServer ns = utils.CreateServerOnPort(1227))
{
c = cf.CreateConnection(o);
Assert.IsTrue(testServers[5].Equals(c.ConnectedUrl));
c.Close();
}
}
示例2: runReqReply
void runReqReply(string testName, long testCount, long testSize)
{
Object pubSubLock = new Object();
byte[] payload = generatePayload(testSize);
ConnectionFactory cf = new ConnectionFactory();
IConnection subConn = cf.CreateConnection(url);
IConnection pubConn = cf.CreateConnection(url);
Thread t = new Thread(() => {
ISyncSubscription s = subConn.SubscribeSync(subject);
for (int i = 0; i < testCount; i++)
{
Msg m = s.NextMessage();
subConn.Publish(m.Reply, payload);
subConn.Flush();
}
});
t.Start();
Thread.Sleep(1000);
Stopwatch sw = sw = Stopwatch.StartNew();
for (int i = 0; i < testCount; i++)
{
pubConn.Request(subject, payload);
}
sw.Stop();
PrintResults(testName, sw, testCount, testSize);
pubConn.Close();
subConn.Close();
}
示例3: runPubSub
void runPubSub(String testName, long testCount, long testSize)
{
Object pubSubLock = new Object();
bool finished = false;
int subCount = 0;
byte[] payload = generatePayload(testSize);
ConnectionFactory cf = new ConnectionFactory();
Options o = ConnectionFactory.GetDefaultOptions();
o.Url = url;
o.SubChannelLength = 10000000;
o.AsyncErrorEventHandler += (sender, obj) => {
System.Console.WriteLine("Error: " + obj.Error);
};
IConnection subConn = cf.CreateConnection(o);
IConnection pubConn = cf.CreateConnection(url);
IAsyncSubscription s = subConn.SubscribeAsync(subject, (sender, args) =>
{
subCount++;
if (subCount == testCount)
{
lock (pubSubLock)
{
finished = true;
Monitor.Pulse(pubSubLock);
}
}
});
s.SetPendingLimits(10000000, 1000000000);
subConn.Flush();
Stopwatch sw = Stopwatch.StartNew();
for (int i = 0; i < testCount; i++)
{
pubConn.Publish(subject, payload);
}
pubConn.Flush();
lock (pubSubLock)
{
if (!finished)
Monitor.Wait(pubSubLock);
}
sw.Stop();
PrintResults(testName, sw, testCount, testSize);
pubConn.Close();
subConn.Close();
}
示例4: runPubSubLatency
// TODO: look into warming up the server, this client for accuracy.
void runPubSubLatency(string testName, long testCount, long testSize)
{
Object subcriberLock = new Object();
bool subscriberDone = false;
List<long> measurements = new List<long>((int)testCount);
byte[] payload = generatePayload(testSize);
ConnectionFactory cf = new ConnectionFactory();
IConnection subConn = cf.CreateConnection(url);
IConnection pubConn = cf.CreateConnection(url);
Stopwatch sw = new Stopwatch();
IAsyncSubscription subs = subConn.SubscribeAsync(subject, (sender, args) =>
{
sw.Stop();
measurements.Add(sw.ElapsedTicks);
lock(subcriberLock)
{
Monitor.Pulse(subcriberLock);
subscriberDone = true;
}
});
subConn.Flush();
for (int i = 0; i < testCount; i++)
{
lock (subcriberLock)
{
subscriberDone = false;
}
sw.Reset();
sw.Start();
pubConn.Publish(subject, payload);
pubConn.Flush();
// block on the subscriber finishing - we do not want any
// overlap in measurements.
lock (subcriberLock)
{
if (!subscriberDone)
{
Monitor.Wait(subcriberLock);
}
}
}
double latencyAvg = measurements.Average();
double stddev = Math.Sqrt(
measurements.Average(
v => Math.Pow((double)v - latencyAvg, 2)
)
);
// TODO: fix accuracy - trim out outliers, etc.
System.Console.WriteLine(
"{0} (us)\t{1} msgs, {2:F2} avg, {3:F2} min, {4:F2} max, {5:F2} stddev",
testName,
testCount,
convertTicksToMicros(latencyAvg),
convertTicksToMicros(measurements.Min()),
convertTicksToMicros(measurements.Max()),
convertTicksToMicros(stddev));
pubConn.Close();
subConn.Close();
}
示例5: TestServersOption
public void TestServersOption()
{
IConnection c = null;
ConnectionFactory cf = new ConnectionFactory();
Options o = utils.DefaultTestOptions;
o.NoRandomize = true;
Assert.ThrowsAny<NATSNoServersException>(() => cf.CreateConnection());
o.Servers = testServers;
Assert.ThrowsAny<NATSNoServersException>(() => cf.CreateConnection(o));
// Make sure we can connect to first server if running
using (NATSServer ns = utils.CreateServerOnPort(1222))
{
c = cf.CreateConnection(o);
Assert.True(testServers[0].Equals(c.ConnectedUrl));
c.Close();
}
// make sure we can connect to a non-first server.
using (NATSServer ns = utils.CreateServerOnPort(1227))
{
c = cf.CreateConnection(o);
Assert.True(testServers[5].Equals(c.ConnectedUrl));
c.Close();
}
}