Ejemplo n.º 1
0
        public Task UpdateScopedConnectionBagsOutSideHub(OnUpdateBagsHubContextEvent theEvent)
        {
            if (theEvent == null)
            {
                throw new ArgumentNullException(nameof(theEvent));
            }

            var hubContext = theEvent.Context;

            if (hubContext == null)
            {
                throw new ArgumentNullException(nameof(theEvent.Context));
            }

            if (theEvent.Bags == null || theEvent.Bags.Count == 0)
            {
                return(Task.CompletedTask);
            }


            var scopedClientKey  = ScopedClientKey.Create().WithScopeGroupId(theEvent.ScopeGroupId).WithClientId(theEvent.ClientId);
            var hubCallerContext = TryGetHubCallerContext(scopedClientKey);

            if (hubCallerContext == null)
            {
                return(Task.CompletedTask);
            }

            var bags = theEvent.Bags;

            var connectionId = hubCallerContext.ConnectionId;
            var conn         = _repository.GetScopedConnection(connectionId);

            if (conn == null)
            {
                return(Task.FromResult(0));
            }

            foreach (var bag in bags)
            {
                conn.Bags[bag.Key] = bag.Value;
            }

            _repository.AddOrUpdateScopedConnection(conn);

            var scopedConnections = _repository.GetScopedConnections(conn.ScopeGroupId);
            var connections       = scopedConnections.OrderBy(x => x.CreateAt).ToList();
            var clientProxy       = hubContext.Clients.Group(conn.ScopeGroupId);

            return(clientProxy.SendAsync(ScopedConst.ForClient.ScopedConnectionsUpdated(), connections));
        }
Ejemplo n.º 2
0
        public async Task OnConnected(OnConnectedEvent theEvent)
        {
            if (theEvent?.RaiseHub == null)
            {
                return;
            }

            var hub = theEvent.RaiseHub;

            var conn         = new ScopedConnection();
            var connectionId = hub.Context.ConnectionId;

            conn.ConnectionId = connectionId;
            var now = DateHelper.Instance.GetDateNow();

            conn.CreateAt     = now;
            conn.LastUpdateAt = now;

            conn.ScopeGroupId = hub.TryGetQueryParameterValue(nameof(conn.ScopeGroupId), string.Empty);
            conn.ClientId     = hub.TryGetQueryParameterValue(nameof(conn.ClientId), string.Empty);
            conn.UpdateDesc();

            _repository.AddOrUpdateScopedConnection(conn);

            if (!string.IsNullOrWhiteSpace(conn.ClientId))
            {
                //scoped clients with same clientId, old should be kicked off
                var scopedClientKey = ScopedClientKey.Create().WithClientId(conn.ClientId).WithScopeGroupId(conn.ScopeGroupId);
                var oneKey          = scopedClientKey.ToOneKey();
                if (ScopedContexts.TryGetValue(oneKey, out var oldClientHub))
                {
                    await KickSameScopedClient(hub, oldClientHub, scopedClientKey).ConfigureAwait(false);
                }
                ScopedContexts[oneKey] = hub.Context;
            }

            await hub.Groups.AddToGroupAsync(conn.ConnectionId, conn.ScopeGroupId).ConfigureAwait(false);

            var scopedConnections = _repository
                                    .GetScopedConnections(conn.ScopeGroupId)
                                    .OrderBy(x => x.CreateAt).ToList();
            var clientProxy = hub.Clients.Group(conn.ScopeGroupId);
            await clientProxy.SendAsync(ScopedConst.ForClient.ScopedConnectionsUpdated(), scopedConnections).ConfigureAwait(false);
        }