例如电报和WhatsApp,例如,当我打开电报时,我想从数据库中将用户获取到聊天应用程序中,如果您的联系人是在线电报,则可以在电报中看到您的联系人,如果该人在线,则表明他/她在线留下聊天电报显示他/她离线。但是有一点关于它,也许突然有一个用户插入数据库,如何立即对用户添加到聊天应用程序中的数据库进行实时跟踪。在前端,我正在使用jquery和ajax。>
[Authorize]
public class ChatHub : Hub
{
private readonly UserManager<AppUser> _userManager;
private readonly ApplicationDbContext _dbContext;
public ChatHub(UserManager<AppUser> userManager,ApplicationDbContext dbContext)
{
_userManager = userManager;
_dbContext = dbContext;
}
public async Task GetAllUsers()
{
var users = await _dbContext.Users.ToListAsync();
}
public async Task SendMessage(string name, string text)
{
var personPricture = "https://devilsworkshop.org/files/2013/01/enlarged-facebook-profile-picture.jpg";
var message = new ChatMessage
{
SenderName = name,
Text = text,
SendAt = DateTimeOffset.Now
};
await Clients.All.SendAsync("ReciveMessage",
message.SenderName,
message.SendAt, message.Text, personPricture);
}
public async Task SendPrivateMessage(string connectionId, string message)
{
await Clients.Client(connectionId).SendAsync("ReceiveMethod", message);
}
private readonly HashSet<string> onlineUsers = new HashSet<string>();
public override async Task OnConnectedAsync()
{
if (Context.User.Identity.IsAuthenticated)
{
var name = Context.User.Identity.Name;
}
onlineUsers.Add(Context.ConnectionId);
await Clients.Caller.SendAsync("UserConnected", Context.ConnectionId);
await base.OnConnectedAsync();
}
public override async Task OnDisconnectedAsync(Exception ex)
{
onlineUsers.Remove(Context.ConnectionId);
await Clients.All.SendAsync("UserDisconnected", Context.ConnectionId);
await base.OnDisconnectedAsync(ex);
}
}
}
[HttpGet,Route("GetAllUsers")]
public async Task<IActionResult> GetAllUsers()
{
List<UsersViewModel> usersList = new List<UsersViewModel>();
var users = await _dbContext.Users.ToListAsync();
foreach (var user in users)
{
usersList.Add(new UsersViewModel
{
UserId = user.Id,
FirstName = user.FirstName,
LastName = user.LastName,
PersonPicture = user.PictureUrl
});
}
return Ok(new { users = usersList });
}
<script>
$(document).ready(function () {
$('#login').click(function () {
let UserName = $('#username').val();
let Password = $('#password').val();
let user = { UserName: UserName, Password: Password };
console.log($('#password').val());
$.ajax({
url: '/api/auth/Login',
type: 'post',
contentType: 'application/json',
data: JSON.stringify(user),
success: function (data) {
let token = data.token;
console.log(token);
url = "/?jwt=" + token;
window.location.href = url;
},
error: function (data) {
console.log(data);
}
})
});
});
</script>
<script>
$(document).ready(function () {
let users = { Id, FirstName, LastName, FullName, PersonPicture };
$.ajax({
type: "get",
url: "/api/auth/GetAllUsers",
contentType: "application/json",
dataType: "json",
data: JSON.stringify(users),
success: function (data) {
$.each(data,
function (index, item) {
let liElement = $("li");
let dFelx_bd_highlight = $('<div class="img_cont"></div>');
let divElement_user_info = $('<div class="user_info"></div>');
let spanElement_FullName = $('<span></span>');
let pElement_Status = $('<p></p>');
let divElement_img_cont = $('<div class="img_cont"></div')
let imgSrc = $('<img src="rounded-circle user_img">');
let spanElement_online_icon = $('<span class="offline_icon">');
divElement_img_cont.append(imgSrc, spanElement_online_icon);
dFelx_bd_highlight.append(divElement_img_cont);
divElement_user_info.append(spanElement_FullName, pElement_Status);
liElement.append(dFelx_bd_highlight, divElement_user_info);
spanElement_FullName.text(item.FullName);
imgSrc.attr('src', item.PersonPicture);
pElement_Status.text(item.FirstName + ' ' + "is offline");
$("ul .contacts").append(liElement);
});
},
error: function (data) {
}
});
});
</script>