Exemplo n.º 1
0
        public override async Task OnConnectedAsync()
        {
            var httpContext = Context.GetHttpContext();

            if (httpContext != null)
            {
                try
                {
                    //Add Logged User
                    int userId = int.Parse(httpContext.Request.Query["userId"].ToString());
                    if (userId > 0)
                    {
                        User user = await _userDBService.GetItemAsync(userId);

                        if (user != null)
                        {
                            var connId = Context.ConnectionId.ToString();
                            _connections.Add(userId, connId);

                            if (!connectedUsers.Any(x => x != null && x.Id == userId))
                            {
                                connectedUsers.Add(user);
                            }

                            //Update Client
                            string usersJson = JsonConvert.SerializeObject(connectedUsers, camelSettings);
                            await Clients.All.SendAsync("UpdateUserList", usersJson);
                        }
                    }
                }
                catch (Exception) { }
            }
        }