示例#1
0
        public override async Task OnConnectedAsync()
        {
            // add user to users online on connect
            var httpContext  = Context.GetHttpContext();
            var username     = httpContext.Request.Query["name"];
            var connectionId = Context.ConnectionId;
            var userToAdd    = new User
            {
                ConnectionId = connectionId,
                Username     = username
            };

            var onlineUsers = await _repo.AddOnlineUser(userToAdd);

            await Clients.All.SendAsync("GetOnlineUsers", onlineUsers);

            var messages = await _repo.GetChatHistory();

            await Clients.Caller.SendAsync("ChatHistory", messages);

            await base.OnConnectedAsync();
        }