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(); }