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


C# MemoryHost.MapConnection方法代码示例

本文整理汇总了C#中Microsoft.AspNet.SignalR.Hosting.Memory.MemoryHost.MapConnection方法的典型用法代码示例。如果您正苦于以下问题:C# MemoryHost.MapConnection方法的具体用法?C# MemoryHost.MapConnection怎么用?C# MemoryHost.MapConnection使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Microsoft.AspNet.SignalR.Hosting.Memory.MemoryHost的用法示例。


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

示例1: ClientGroupsSyncWithServerGroupsOnReconnectLongPolling

        public static IDisposable ClientGroupsSyncWithServerGroupsOnReconnectLongPolling()
        {
            var host = new MemoryHost();

            host.Configuration.KeepAlive = null;
            host.Configuration.ConnectionTimeout = TimeSpan.FromSeconds(5);
            host.Configuration.HeartbeatInterval = TimeSpan.FromSeconds(2);
            host.MapConnection<MyRejoinGroupConnection>("/groups");

            var connection = new Client.Connection("http://foo/groups");
            var inGroupOnReconnect = new List<bool>();
            var wh = new ManualResetEventSlim();

            connection.Received += message =>
            {
                Console.WriteLine(message);
                wh.Set();
            };

            connection.Reconnected += () =>
            {
                var inGroup = connection.Groups.Contains(typeof(MyRejoinGroupConnection).FullName + ".test");

                if (!inGroup)
                {
                    Debugger.Break();
                }

                inGroupOnReconnect.Add(inGroup);

                connection.Send(new { type = 3, group = "test", message = "Reconnected" }).Wait();
            };

            connection.Start(new Client.Transports.LongPollingTransport(host)).Wait();

            // Join the group
            connection.Send(new { type = 1, group = "test" }).Wait();

            Thread.Sleep(TimeSpan.FromSeconds(10));

            if (!wh.Wait(TimeSpan.FromSeconds(10)))
            {
                Debugger.Break();
            }

            Console.WriteLine(inGroupOnReconnect.Count > 0);
            Console.WriteLine(String.Join(", ", inGroupOnReconnect.Select(b => b.ToString())));

            connection.Stop();

            return host;
        }
开发者ID:rustd,项目名称:SignalR,代码行数:52,代码来源:StressRuns.cs

示例2: Run

        public static IDisposable Run(int connections, int senders, string payload, string transport)
        {
            var host = new MemoryHost();
            // Let's not run out of memory everytime
            host.MapConnection<StressConnection>("/echo");
            var countDown = new CountdownEvent(senders);
            var cancellationTokenSource = new CancellationTokenSource();

            for (int i = 0; i < connections; i++)
            {
                if (transport.Equals("longPolling", StringComparison.OrdinalIgnoreCase))
                {
                    ThreadPool.QueueUserWorkItem(state =>
                    {
                        string connectionId = state.ToString();
                        LongPollingLoop(host, connectionId);

                    }, i);
                }
                else
                {
                    string connectionId = i.ToString();
                    ProcessRequest(host, transport, connectionId);
                }
            }

            for (var i = 0; i < senders; i++)
            {
                ThreadPool.QueueUserWorkItem(_ =>
                {
                    while (!cancellationTokenSource.IsCancellationRequested)
                    {
                        string connectionId = i.ToString();
                        ProcessSendRequest(host, transport, connectionId, payload);
                    }

                    countDown.Signal();
                });
            }

            return new DisposableAction(() =>
            {
                cancellationTokenSource.Cancel();

                // Wait for all senders to stop
                countDown.Wait(TimeSpan.FromMilliseconds(1000 * senders));

                host.Dispose();
            });
        }
开发者ID:nonintanon,项目名称:SignalR,代码行数:50,代码来源:MemoryHostRun.cs

示例3: ThrownWebExceptionShouldBeUnwrapped

            public void ThrownWebExceptionShouldBeUnwrapped()
            {
                var host = new MemoryHost();
                host.MapConnection<MyBadConnection>("/ErrorsAreFun");

                var connection = new Client.Connection("http://test/ErrorsAreFun");

                // Expecting 404
                var aggEx = Assert.Throws<AggregateException>(() => connection.Start(host).Wait());

                connection.Stop();

                using (var ser = aggEx.GetError())
                {
                    Assert.Equal(ser.StatusCode, HttpStatusCode.NotFound);
                    Assert.NotNull(ser.ResponseBody);
                    Assert.NotNull(ser.Exception);
                }
            }
开发者ID:ntcnet83,项目名称:SignalR,代码行数:19,代码来源:ConnectionFacts.cs

示例4: DisconnectFiresForPersistentConnectionWhenClientGoesAway

        public void DisconnectFiresForPersistentConnectionWhenClientGoesAway()
        {
            var host = new MemoryHost();
            host.MapConnection<MyConnection>("/echo");
            host.Configuration.DisconnectTimeout = TimeSpan.Zero;
            host.Configuration.HeartBeatInterval = TimeSpan.FromSeconds(5);
            var connectWh = new ManualResetEventSlim();
            var disconnectWh = new ManualResetEventSlim();
            host.DependencyResolver.Register(typeof(MyConnection), () => new MyConnection(connectWh, disconnectWh));
            var connection = new Client.Connection("http://foo/echo");

            // Maximum wait time for disconnect to fire (3 heart beat intervals)
            var disconnectWait = TimeSpan.FromTicks(host.Configuration.HeartBeatInterval.Ticks * 3);

            connection.Start(host).Wait();

            Assert.True(connectWh.Wait(TimeSpan.FromSeconds(10)), "Connect never fired");

            connection.Stop();

            Assert.True(disconnectWh.Wait(disconnectWait), "Disconnect never fired");
        }
开发者ID:ntcnet83,项目名称:SignalR,代码行数:22,代码来源:DisconnectFacts.cs

示例5: RunMemoryHost

        private static MemoryHost RunMemoryHost()
        {
            var host = new MemoryHost();
            host.MapConnection<StressConnection>("/echo");

            string payload = GetPayload();

            MeasureStats((MessageBus)host.DependencyResolver.Resolve<IMessageBus>());

            Action<PersistentResponse> handler = (r) =>
            {
                Interlocked.Add(ref _received, r.TotalCount);
                Interlocked.Add(ref _avgLastReceivedCount, r.TotalCount);
            };

            LongPollingTransport.SendingResponse += handler;
            ForeverFrameTransport.SendingResponse += handler;

            for (int i = 0; i < _clients; i++)
            {
                ThreadPool.QueueUserWorkItem(state =>
                {
                    Interlocked.Increment(ref _clientsRunning);
                    string connectionId = state.ToString();

                    //LongPollingLoop(host, connectionId);
                    ProcessRequest(host, "serverSentEvents", connectionId);
                }, i);
            }

            for (var i = 1; i <= _senders; i++)
            {
                ThreadPool.QueueUserWorkItem(_ =>
                {
                    var context = host.ConnectionManager.GetConnectionContext<StressConnection>();
                    StartSendLoop(i.ToString(), (source, key, value) => context.Connection.Broadcast(value), payload);
                });
            }

            return host;
        }
开发者ID:ntcnet83,项目名称:SignalR,代码行数:41,代码来源:Program.cs

示例6: SendToAllButCaller

            public void SendToAllButCaller()
            {
                using (var host = new MemoryHost())
                {
                    host.MapConnection<FilteredConnection>("/filter");
                    var connection1 = new Client.Connection("http://foo/filter");
                    var connection2 = new Client.Connection("http://foo/filter");

                    var wh1 = new ManualResetEventSlim(initialState: false);
                    var wh2 = new ManualResetEventSlim(initialState: false);

                    connection1.Received += data => wh1.Set();
                    connection2.Received += data => wh2.Set();

                    connection1.Start(host).Wait();
                    connection2.Start(host).Wait();

                    connection1.Send("test").Wait();

                    Assert.False(wh1.WaitHandle.WaitOne(TimeSpan.FromSeconds(5)));
                    Assert.True(wh2.WaitHandle.WaitOne(TimeSpan.FromSeconds(5)));

                    connection1.Stop();
                    connection2.Stop();
                }
            }
开发者ID:nonintanon,项目名称:SignalR,代码行数:26,代码来源:PersistentConnectionFacts.cs

示例7: ReconnectFiresAfterTimeOutSSE

            public void ReconnectFiresAfterTimeOutSSE()
            {
                using (var host = new MemoryHost())
                {
                    var conn = new MyReconnect();
                    host.Configuration.KeepAlive = null;
                    host.Configuration.ConnectionTimeout = TimeSpan.FromSeconds(5);
                    host.Configuration.HeartBeatInterval = TimeSpan.FromSeconds(5);
                    host.DependencyResolver.Register(typeof(MyReconnect), () => conn);
                    host.MapConnection<MyReconnect>("/endpoint");

                    var connection = new Client.Connection("http://foo/endpoint");
                    connection.Start(new Client.Transports.ServerSentEventsTransport(host)).Wait();

                    Thread.Sleep(TimeSpan.FromSeconds(15));

                    connection.Stop();

                    Assert.InRange(conn.Reconnects, 1, 4);
                }
            }
开发者ID:nonintanon,项目名称:SignalR,代码行数:21,代码来源:PersistentConnectionFacts.cs

示例8: ReconnectFiresAfterHostShutDown

            public void ReconnectFiresAfterHostShutDown()
            {
                using (var host = new MemoryHost())
                {
                    var conn = new MyReconnect();
                    host.DependencyResolver.Register(typeof(MyReconnect), () => conn);
                    host.MapConnection<MyReconnect>("/endpoint");

                    var connection = new Client.Connection("http://foo/endpoint");
                    connection.Start(host).Wait();

                    host.Dispose();

                    Thread.Sleep(TimeSpan.FromSeconds(5));

                    Assert.Equal(Client.ConnectionState.Reconnecting, connection.State);

                    connection.Stop();
                }
            }
开发者ID:nonintanon,项目名称:SignalR,代码行数:20,代码来源:PersistentConnectionFacts.cs

示例9: SendRaisesOnReceivedFromAllEvents

            public void SendRaisesOnReceivedFromAllEvents()
            {
                using (var host = new MemoryHost())
                {
                    host.MapConnection<MySendingConnection>("/multisend");

                    var connection = new Client.Connection("http://foo/multisend");
                    var results = new List<string>();
                    connection.Received += data =>
                    {
                        results.Add(data);
                    };

                    connection.Start(host).Wait();
                    connection.Send("").Wait();

                    Thread.Sleep(TimeSpan.FromSeconds(5));

                    connection.Stop();

                    Debug.WriteLine(String.Join(", ", results));

                    Assert.Equal(4, results.Count);
                    Assert.Equal("OnConnectedAsync1", results[0]);
                    Assert.Equal("OnConnectedAsync2", results[1]);
                    Assert.Equal("OnReceivedAsync1", results[2]);
                    Assert.Equal("OnReceivedAsync2", results[3]);
                }
            }
开发者ID:nonintanon,项目名称:SignalR,代码行数:29,代码来源:PersistentConnectionFacts.cs

示例10: SendCanBeCalledAfterStateChangedEvent

            public void SendCanBeCalledAfterStateChangedEvent()
            {
                using (var host = new MemoryHost())
                {
                    host.MapConnection<MySendingConnection>("/multisend");

                    var connection = new Client.Connection("http://foo/multisend");
                    var results = new List<string>();
                    connection.Received += data =>
                    {
                        results.Add(data);
                    };

                    connection.StateChanged += stateChange =>
                    {
                        if (stateChange.NewState == Client.ConnectionState.Connected)
                        {
                            connection.Send("").Wait();
                        }
                    };

                    connection.Start(host).Wait();

                    Thread.Sleep(TimeSpan.FromSeconds(5));

                    connection.Stop();

                    Debug.WriteLine(String.Join(", ", results));

                    Assert.Equal(4, results.Count);
                }
            }
开发者ID:nonintanon,项目名称:SignalR,代码行数:32,代码来源:PersistentConnectionFacts.cs

示例11: GroupsAreNotReadOnConnectedAsyncLongPolling

            public void GroupsAreNotReadOnConnectedAsyncLongPolling()
            {
                using (var host = new MemoryHost())
                {
                    host.MapConnection<MyConnection>("/echo");

                    var connection = new Client.Connection("http://foo/echo");
                    connection.Groups = new List<string> { typeof(MyConnection).FullName + ".test" };
                    connection.Received += data =>
                    {
                        Assert.False(true, "Unexpectedly received data");
                    };

                    var transport = new Client.Transports.LongPollingTransport(host);
                    connection.Start(transport).Wait();

                    Thread.Sleep(TimeSpan.FromSeconds(5));

                    connection.Stop();
                }
            }
开发者ID:nonintanon,项目名称:SignalR,代码行数:21,代码来源:PersistentConnectionFacts.cs

示例12: ConnectionsWithTheSameConnectionIdSSECloseGracefully

            public void ConnectionsWithTheSameConnectionIdSSECloseGracefully()
            {
                using (var host = new MemoryHost())
                {
                    host.MapConnection<MyConnection>("/echo");

                    var tasks = new List<Task>();

                    for (int i = 0; i < 1000; i++)
                    {
                        tasks.Add(ProcessRequest(host, "serverSentEvents", "1"));
                    }

                    ProcessRequest(host, "serverSentEvents", "1");

                    Task.WaitAll(tasks.ToArray());

                    Assert.True(tasks.All(t => !t.IsFaulted));
                }
            }
开发者ID:nonintanon,项目名称:SignalR,代码行数:20,代码来源:PersistentConnectionFacts.cs

示例13: GroupsRejoinedWhenOnRejoiningGroupsOverridden

            public void GroupsRejoinedWhenOnRejoiningGroupsOverridden()
            {
                using (var host = new MemoryHost())
                {
                    host.Configuration.KeepAlive = null;
                    host.Configuration.ConnectionTimeout = TimeSpan.FromSeconds(2);
                    host.Configuration.HeartBeatInterval = TimeSpan.FromSeconds(2);
                    host.MapConnection<MyRejoinGroupConnection>("/groups");

                    var connection = new Client.Connection("http://foo/groups");
                    var list = new List<string>();
                    connection.Received += data =>
                    {
                        list.Add(data);
                    };

                    connection.Start(host).Wait();

                    // Join the group
                    connection.Send(new { type = 1, group = "test" }).Wait();

                    // Sent a message
                    connection.Send(new { type = 3, group = "test", message = "hello to group test" }).Wait();

                    // Force Reconnect
                    Thread.Sleep(TimeSpan.FromSeconds(5));

                    // Send a message
                    connection.Send(new { type = 3, group = "test", message = "goodbye to group test" }).Wait();

                    Thread.Sleep(TimeSpan.FromSeconds(5));

                    connection.Stop();

                    Assert.Equal(2, list.Count);
                    Assert.Equal("hello to group test", list[0]);
                    Assert.Equal("goodbye to group test", list[1]);
                }
            }
开发者ID:nonintanon,项目名称:SignalR,代码行数:39,代码来源:PersistentConnectionFacts.cs

示例14: GroupsReceiveMessages

            public void GroupsReceiveMessages()
            {
                using (var host = new MemoryHost())
                {
                    host.MapConnection<MyGroupConnection>("/groups");

                    var connection = new Client.Connection("http://foo/groups");
                    var list = new List<string>();
                    connection.Received += data =>
                    {
                        list.Add(data);
                    };

                    connection.Start(host).Wait();

                    // Join the group
                    connection.Send(new { type = 1, group = "test" }).Wait();

                    // Sent a message
                    connection.Send(new { type = 3, group = "test", message = "hello to group test" }).Wait();

                    // Leave the group
                    connection.Send(new { type = 2, group = "test" }).Wait();

                    // Send a message
                    connection.Send(new { type = 3, group = "test", message = "goodbye to group test" }).Wait();

                    Thread.Sleep(TimeSpan.FromSeconds(5));

                    connection.Stop();

                    Assert.Equal(1, list.Count);
                    Assert.Equal("hello to group test", list[0]);
                }
            }
开发者ID:nonintanon,项目名称:SignalR,代码行数:35,代码来源:PersistentConnectionFacts.cs

示例15: RunInMemoryHost

        private static void RunInMemoryHost()
        {
            var host = new MemoryHost();
            host.MapConnection<MyConnection>("/echo");

            var connection = new Connection("http://foo/echo");

            connection.Received += data =>
            {
                Console.WriteLine(data);
            };

            connection.StateChanged += change =>
            {
                Console.WriteLine(change.OldState + " => " + change.NewState);
            };

            connection.Start(host).Wait();

            ThreadPool.QueueUserWorkItem(_ =>
            {
                try
                {
                    while (true)
                    {
                        connection.Send(DateTime.Now.ToString());

                        Thread.Sleep(2000);
                    }
                }
                catch
                {

                }
            });
        }
开发者ID:nonintanon,项目名称:SignalR,代码行数:36,代码来源:Program.cs


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