Exemplo n.º 1
0
        public override Task OnConnectedAsync()
        {
            var user = JsonConvert.DeserializeObject <User>(Context.GetHttpContext().Request.Query["user"]);

            _connectionsRepository.Add(Context.ConnectionId, user);
            Clients.All.SendAsync("chat", _connectionsRepository.GetAllUser(), user);
            return(base.OnConnectedAsync());
        }
        protected async Task _DoSubscribeAsync <TEvent>(string toEvent, Func <TEvent, Task> subscriptionHandler)
            where TEvent : CqrsEvent
        {
            var connection = _connection.Value;

            connection.On(toEvent, subscriptionHandler);
            if (connection.State == HubConnectionState.Disconnected)
            {
                var currentUserPrincipalName = await GetCurrentUserPrincipalName();

                var connectionInfo = new ConnectionInfo {
                    UserId = currentUserPrincipalName
                };
                connection.Closed += async e => { await _connectionsRepository.Remove(connectionInfo); };

                await connection.StartAsync();

                connectionInfo.ConnectionId = connection.ConnectionId;
                await _connectionsRepository.Add(connectionInfo);
            }
        }