Exemplo n.º 1
0
        public static ScopedConnection Create(string scopeGroupId, string clientId, string connectionId)
        {
            var context = new ScopedConnection();

            context.ClientId     = clientId ?? string.Empty;
            context.ScopeGroupId = scopeGroupId ?? string.Empty;
            context.ConnectionId = connectionId ?? string.Empty;
            return(context);
        }
Exemplo n.º 2
0
        public static ScopedConnection TryParseFromHubCallerContext(this HubCallerContext callerContext, params string[] queryParams)
        {
            var context = new ScopedConnection();

            context.ConnectionId = callerContext.ConnectionId;
            context.ScopeGroupId = callerContext.TryGetQueryParameterValue(nameof(context.ScopeGroupId), string.Empty);
            context.ClientId     = callerContext.TryGetQueryParameterValue(nameof(context.ClientId), string.Empty);
            foreach (var queryParam in queryParams)
            {
                var queryValue = callerContext.TryGetQueryParameterValue <object>(queryParam, null);
                context.SetBagValue(queryParam, queryValue);
            }
            return(context);
        }
Exemplo n.º 3
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);
        }
        public void AddOrUpdateScopedConnection(ScopedConnection conn)
        {
            if (conn == null)
            {
                return;
            }

            Scopes.TryGetValue(conn.ScopeGroupId, out var connDic);
            if (connDic == null)
            {
                connDic = new ConcurrentDictionary <string, ScopedConnection>(StringComparer.OrdinalIgnoreCase);
                Scopes[conn.ScopeGroupId] = connDic;
            }

            connDic[conn.ConnectionId]          = conn;
            ConnectionScopes[conn.ConnectionId] = conn.ScopeGroupId;
            conn.LastUpdateAt = DateHelper.Instance.GetDateNow();
            conn.UpdateDesc();
        }