Ejemplo n.º 1
0
        public void Test_All()
        {
            int tenantId = 1;

            Dictionary <string, int> connections = new Dictionary <string, int>();

            for (int i = 0; i < 100; i++)
            {
                connections.Add(MakeNewConnectionId(), i + 1);
            }

            foreach (var pair in connections)
            {
                _clientManager.Add(new OnlineClient(pair.Key, "127.0.0.1", tenantId, pair.Value));
            }

            var testId = connections.Keys.ToList()[5];

            _clientManager.GetAllClients().Count.ShouldBe(connections.Count);
            _clientManager.GetAllByUserId(new UserIdentifier(tenantId, connections[testId])).Count.ShouldBe(1);
            _clientManager.GetByConnectionIdOrNull(testId).ShouldNotBeNull();
            _clientManager.Remove(testId).ShouldBeTrue();
            _clientManager.GetAllClients().Count.ShouldBe(connections.Count - 1);
            _clientManager.GetByConnectionIdOrNull(testId).ShouldBeNull();
            _clientManager.GetAllByUserId(new UserIdentifier(tenantId, connections[testId])).Count.ShouldBe(0);
        }
Ejemplo n.º 2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public async Task InitUser(long userId)
        {
            var oneClient = _onlineClientManager.GetByConnectionIdOrNull(Context.ConnectionId);

            if (oneClient != null)
            {
                _onlineClientManager.Remove(Context.ConnectionId);
                var newclient = CreateClientForCurrentConnection(userId);
                _onlineClientManager.Add(newclient);
            }
            else
            {
            }
            //await Clients.All.InvokeAsync("NoticeOnline", $"用户组数据更新完成,新增id为:{Context.ConnectionId}userId:{userId}");
        }
Ejemplo n.º 3
0
        public override async Task OnReconnected()
        {
            await base.OnReconnected();

            var client = _onlineClientManager.GetByConnectionIdOrNull(Context.ConnectionId);
            if (client == null)
            {
                client = CreateClientForCurrentConnection();
                _onlineClientManager.Add(client);
                Logger.Debug("A client is connected (on reconnected event): " + client);
            }
            else
            {
                Logger.Debug("A client is reconnected: " + client);
            }
        }