У меня есть приложение MVC.
Я реализовал singalR для получения живых уведомлений, но как получать только пользовательские уведомления.
NotificationSend.cs
public class NotificationSend : Hub
{
private static IHubContext hubContext = GlobalHost.ConnectionManager.GetHubContext<NotificationSend>();
public static ConcurrentDictionary<string, MyUserType> MyUsers = new ConcurrentDictionary<string, MyUserType>();
public override Task OnConnected()
{
MyUsers.TryAdd(Context.ConnectionId, new MyUserType() { ConnectionId = Context.ConnectionId });
return base.OnConnected();
}
public override Task OnDisconnected(bool stopCalled)
{
MyUserType garbage;
MyUsers.TryRemove(Context.ConnectionId, out garbage);
return base.OnDisconnected(stopCalled);
}
public static void SendToUser(string messageText)
{
hubContext.Clients.Client(MyUsers.Keys.ToList().FirstOrDefault()).Notification(messageText);
}
public static void StopLoader(string messageText)
{
hubContext.Clients.Client(MyUsers.Keys.ToList().FirstOrDefault()).Stoploader(messageText);
}
}
public class MyUserType
{
public string ConnectionId { get; set; }
}
HomeController.cs
public class HomeController : Controller
{
public async Task<ActionResult> SaveData()
{
foreach (var mydata in DataList)
{
// save data code and show below message on UI
NotificationSend.SendToUser(mydata.Name + ": Data saved");
Я могу получать уведомления об интерфейсе совершенно нормально, но проблема в том, что
Если пользователь А использует свой собственный компьютер и свой логин, он должен получить только свойуведомление, я знаю, что URL веб-приложения такой же.
для этого я внесу изменения ниже, но после этого изменения не видно никаких уведомлений.
string UserID = User.Identity.Name;
hubContext.Clients.User(UserID).Notification(mydata.Name + ": Data saved");
Layout.js
$(function () {
var notification = $.connection.notificationSend;
console.log(notification);
notification.client.Notification = function (Count) {
$('#liveupdate').empty();
$('#liveupdate').show();
$('#liveupdate').append(Count);
};
$.connection.hub.start().done(function () {
var connectionId = $.connection.hub.id;
console.log("Connected Successfully");
}).fail(function (response) {
console.log("not connected" + response);
});
});