当前位置: 首页>>代码示例>>C#>>正文


C# Client.ConnectionFactory类代码示例

本文整理汇总了C#中NATS.Client.ConnectionFactory的典型用法代码示例。如果您正苦于以下问题:C# ConnectionFactory类的具体用法?C# ConnectionFactory怎么用?C# ConnectionFactory使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


ConnectionFactory类属于NATS.Client命名空间,在下文中一共展示了ConnectionFactory类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: TestClientAutoUnsub

        public void TestClientAutoUnsub()
        {
            using (IConnection c = new ConnectionFactory().CreateConnection())
            {
                long received = 0;
                int max = 10;

                using (ISyncSubscription s = c.SubscribeSync("foo"))
                {
                    s.AutoUnsubscribe(max);

                    for (int i = 0; i < max * 2; i++)
                    {
                        c.Publish("foo", null);
                    }
                    c.Flush();

                    Thread.Sleep(100);

                    try
                    {
                        while (true)
                        {
                            s.NextMessage(0);
                            received++;
                        }
                    }
                    catch (NATSBadSubscriptionException) { /* ignore */ }

                    Assert.IsTrue(received == max);
                    Assert.IsFalse(s.IsValid);
                }
            }
        }
开发者ID:bendan365,项目名称:csnats-wip,代码行数:34,代码来源:UnitTestSub.cs

示例2: TestReconnectDisallowedFlags

        public void TestReconnectDisallowedFlags()
        {
            Options opts = ConnectionFactory.GetDefaultOptions();
            opts.Url = "nats://localhost:22222";
            opts.AllowReconnect = false;

            Object testLock = new Object();

            opts.ClosedEventHandler = (sender, args) =>
            {
                lock(testLock)
                {
                    Monitor.Pulse(testLock);
                }
            };

            using (NATSServer ns = utils.CreateServerOnPort(22222))
            {
                using (IConnection c = new ConnectionFactory().CreateConnection(opts))
                {
                    lock (testLock)
                    {
                        ns.Shutdown();
                        Assert.IsTrue(Monitor.Wait(testLock, 1000));
                    }
                }
            }
        }
开发者ID:bendan365,项目名称:csnats-wip,代码行数:28,代码来源:UnitTestReconnect.cs

示例3: TestCloseDisconnectedHandler

        public void TestCloseDisconnectedHandler()
        {
            bool disconnected = false;
            Object mu = new Object();

            Options o = ConnectionFactory.GetDefaultOptions();
            o.AllowReconnect = false;
            o.DisconnectedEventHandler += (sender, args) => {
                lock (mu)
                {
                    disconnected = true;
                    Monitor.Pulse(mu);
                }
            };

            IConnection c = new ConnectionFactory().CreateConnection(o);
            lock (mu)
            {
                c.Close();
                Monitor.Wait(mu, 20000);
            }
            Assert.IsTrue(disconnected);

            // now test using.
            disconnected = false;
            lock (mu)
            {
                using (c = new ConnectionFactory().CreateConnection(o)) { };
                Monitor.Wait(mu);
            }
            Assert.IsTrue(disconnected);
        }
开发者ID:bendan365,项目名称:csnats-wip,代码行数:32,代码来源:UnitTestConn.cs

示例4: Run

        public void Run(string[] args)
        {
            Stopwatch sw = null;

            parseArgs(args);
            banner();

            Options opts = ConnectionFactory.GetDefaultOptions();
            opts.Url = url;

            using (IConnection c = new ConnectionFactory().CreateConnection(opts))
            {
                sw = Stopwatch.StartNew();

                for (int i = 0; i < count; i++)
                {
                    c.Request(subject, payload);
                }
                c.Flush();

                sw.Stop();

                System.Console.Write("Completed {0} requests in {1} seconds ", count, sw.Elapsed.TotalSeconds);
                System.Console.WriteLine("({0} requests/second).",
                    (int)(count / sw.Elapsed.TotalSeconds));
                printStats(c);

            }
        }
开发者ID:bendan365,项目名称:csnats-wip,代码行数:29,代码来源:Requestor.cs

示例5: TestConnectionStatus

 public void TestConnectionStatus()
 {
     IConnection c = new ConnectionFactory().CreateConnection();
     Assert.AreEqual(ConnState.CONNECTED, c.State);
     c.Close();
     Assert.AreEqual(ConnState.CLOSED, c.State);
 }
开发者ID:bendan365,项目名称:csnats-wip,代码行数:7,代码来源:UnitTestConn.cs

示例6: TestAsyncSubHandlerAPI

        public void TestAsyncSubHandlerAPI()
        {
            using (IConnection c = new ConnectionFactory().CreateConnection())
            {
                int received = 0;

                EventHandler<MsgHandlerEventArgs> h = (sender, args) =>
                {
                    Interlocked.Increment(ref received);
                };

                using (IAsyncSubscription s = c.SubscribeAsync("foo", h))
                {
                    c.Publish("foo", null);
                    c.Flush();
                    Thread.Sleep(500);
                }

                using (IAsyncSubscription s = c.SubscribeAsync("foo", "bar", h))
                {
                    c.Publish("foo", null);
                    c.Flush();
                    Thread.Sleep(500);
                }

                if (received != 2)
                {
                    Assert.Fail("Received ({0}) != 2", received);
                }
            }
        }
开发者ID:ColinSullivan1,项目名称:csnats,代码行数:31,代码来源:UnitTestBasic.cs

示例7: TestBasicReconnectFunctionality

        public void TestBasicReconnectFunctionality()
        {
            Options opts = utils.DefaultTestOptions;
            opts.Url = "nats://localhost:22222";
            opts.MaxReconnect = 2;
            opts.ReconnectWait = 1000;

            Object testLock = new Object();
            Object msgLock = new Object();

            opts.DisconnectedEventHandler = (sender, args) =>
            {
                lock (testLock)
                {
                    Monitor.Pulse(testLock);
                }
            };

            opts.ReconnectedEventHandler = (sender, args) =>
            {
                // NOOP
            };

            NATSServer ns = utils.CreateServerOnPort(22222);

            using (IConnection c = new ConnectionFactory().CreateConnection(opts))
            {
                IAsyncSubscription s = c.SubscribeAsync("foo");
                s.MessageHandler += (sender, args) =>
                {
                    lock (msgLock)
                    {
                        Monitor.Pulse(msgLock);
                    }
                };

                s.Start();
                c.Flush();

                lock (testLock)
                {
                    ns.Shutdown();
                    Assert.True(Monitor.Wait(testLock, 100000));
                }

                c.Publish("foo", Encoding.UTF8.GetBytes("Hello"));

                // restart the server.
                using (ns = utils.CreateServerOnPort(22222))
                {
                    lock (msgLock)
                    {
                        c.Flush(50000);
                        Assert.True(Monitor.Wait(msgLock, 10000));
                    }

                    Assert.True(c.Stats.Reconnects == 1);
                }
            }
        }
开发者ID:nats-io,项目名称:csnats,代码行数:60,代码来源:UnitTestReconnect.cs

示例8: 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();
            }
        }
开发者ID:bendan365,项目名称:csnats-wip,代码行数:34,代码来源:UnitTestCluster.cs

示例9: Run

        public void Run(string[] args)
        {
            parseArgs(args);
            banner();

            Options opts = ConnectionFactory.GetDefaultOptions();
            opts.Url = url;

            replyMsg.Data = Encoding.UTF8.GetBytes("reply");

            using (IConnection c = new ConnectionFactory().CreateConnection(opts))
            {
                TimeSpan elapsed;

                if (sync)
                {
                    elapsed = receiveSyncSubscriber(c);
                }
                else
                {
                    elapsed = receiveAsyncSubscriber(c);
                }

                System.Console.Write("Replied to {0} msgs in {1} seconds ", received, elapsed.TotalSeconds);
                System.Console.WriteLine("({0} replies/second).",
                    (int)(received / elapsed.TotalSeconds));
                printStats(c);

            }
        }
开发者ID:nats-io,项目名称:csnats,代码行数:30,代码来源:Replier.cs

示例10: connectAndFail

        private void connectAndFail(String url)
        {
            try
            {
                System.Console.WriteLine("Trying: " + url);

                hitDisconnect = 0;
                Options opts = ConnectionFactory.GetDefaultOptions();
                opts.Url = url;
                opts.DisconnectedEventHandler += handleDisconnect;
                IConnection c = new ConnectionFactory().CreateConnection(url);

                Assert.Fail("Expected a failure; did not receive one");
                
                c.Close();
            }
            catch (Exception e)
            {
                if (e.Message.Contains("Authorization"))
                {
                    System.Console.WriteLine("Success with expected failure: " + e.Message);
                }
                else
                {
                    Assert.Fail("Unexpected exception thrown: " + e);
                }
            }
            finally
            {
                if (hitDisconnect > 0)
                    Assert.Fail("The disconnect event handler was incorrectly invoked.");
            }
        }
开发者ID:bendan365,项目名称:csnats-wip,代码行数:33,代码来源:UnitTestAuth.cs

示例11: TestAuthServers

        public void TestAuthServers()
        {
            string[] plainServers = new string[] {
                "nats://localhost:1222",
                "nats://localhost:1224"
            };

            Options opts = utils.DefaultTestOptions;
            opts.NoRandomize = true;
            opts.Servers = plainServers;
            opts.Timeout = 5000;

            using (NATSServer as1 = utils.CreateServerWithConfig("auth_1222.conf"),
                              as2 = utils.CreateServerWithConfig("auth_1224.conf"))
            {
                Assert.ThrowsAny<NATSException>(() => new ConnectionFactory().CreateConnection(opts));

                // Test that we can connect to a subsequent correct server.
                string[] authServers = new string[] {
                    "nats://localhost:1222",
                    "nats://username:[email protected]:1224"};

                opts.Servers = authServers;

                using (IConnection c = new ConnectionFactory().CreateConnection(opts))
                {
                    Assert.Equal(authServers[1], c.ConnectedUrl);
                }
            }
        }
开发者ID:nats-io,项目名称:csnats,代码行数:30,代码来源:UnitTestCluster.cs

示例12: TestSimplePublish

 public void TestSimplePublish()
 {
     using (IConnection c = new ConnectionFactory().CreateConnection())
     {
         c.Publish("foo", Encoding.UTF8.GetBytes("Hello World!"));
     }
 }
开发者ID:bendan365,项目名称:csnats-wip,代码行数:7,代码来源:UnitTestBasic.cs

示例13: TestServerAutoUnsub

        public void TestServerAutoUnsub()
        {
            using (IConnection c = new ConnectionFactory().CreateConnection())
            {
                long received = 0;
                int max = 10;

                using (IAsyncSubscription s = c.SubscribeAsync("foo"))
                {
                    s.MessageHandler += (sender, arg) =>
                    {
                        System.Console.WriteLine("Received msg.");
                        received++;
                    };

                    s.AutoUnsubscribe(max);
                    s.Start();

                    for (int i = 0; i < (max * 2); i++)
                    {
                        c.Publish("foo", Encoding.UTF8.GetBytes("hello"));
                    }
                    c.Flush();

                    Thread.Sleep(500);

                    if (received != max)
                    {
                        Assert.Fail("Recieved ({0}) != max ({1})",
                            received, max);
                    }
                    Assert.IsFalse(s.IsValid);
                }
            }
        }
开发者ID:bendan365,项目名称:csnats-wip,代码行数:35,代码来源:UnitTestSub.cs

示例14: Run

        public void Run(string[] args)
        {
            parseArgs(args);
            banner();

            Options opts = ConnectionFactory.GetDefaultOptions();
            opts.Url = url;

            using (IConnection c = new ConnectionFactory().CreateConnection(opts))
            {
                TimeSpan elapsed;

                if (sync)
                {
                    elapsed = receiveSyncSubscriber(c);
                }
                else
                {
                    elapsed = receiveAsyncSubscriber(c);
                }

                System.Console.Write("Received {0} msgs in {1} seconds ", count, elapsed.TotalSeconds);
                System.Console.WriteLine("({0} msgs/second).",
                    (int)(count / elapsed.TotalSeconds));
                printStats(c);

            }
        }
开发者ID:bendan365,项目名称:csnats-wip,代码行数:28,代码来源:QueueGroup.cs

示例15: TestCustomObjectSerialization

        public void TestCustomObjectSerialization()
        {
            using (IEncodedConnection c = new ConnectionFactory().CreateEncodedConnection())
            {
                Object mu = new Object();
                SerializationTestObj origObj = new SerializationTestObj();

                EventHandler<EncodedMessageEventArgs> eh = (sender, args) =>
                {
                    // Ensure we blow up in the cast
                    SerializationTestObj so = (SerializationTestObj)args.ReceivedObject;
                    Assert.IsTrue(so.Equals(origObj));

                    lock (mu)
                    {
                        Monitor.Pulse(mu);
                    }
                };

                using (IAsyncSubscription s = c.SubscribeAsync("foo", eh))
                {
                    lock (mu)
                    {
                        c.Publish("foo", new SerializationTestObj());
                        c.Flush();

                        Monitor.Wait(mu, 1000);
                    }
                }
            }
        }
开发者ID:ColinSullivan1,项目名称:csnats,代码行数:31,代码来源:UnitTestEncoding.cs


注:本文中的NATS.Client.ConnectionFactory类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。