如何实现SignalR显示电子邮件通知到接收者的ASP.NET MVC 4应用
我有一个要求,以显示电子邮件通知使用SignalR接收器。我正在开发使用MVC 4.我是一个初学者,不具备SignalR很多知识我的申请。到目前为止,我已经跟着下面的帖子,试图prepare能满足我的要求的样本。
I have a requirement to show email notification to receivers using SignalR. I am developing my application using MVC 4. I am a beginner and do not have much knowledge for SignalR. So far I have followed the posts below and tried to prepare a sample that can fulfill my requirement.
的http://www.$c$cproject.com/Articles/732190/Real-Time-Web-Solution-for-Chat-by-MVC-SignalR-H
http://techbrij.com/realtime-post-comment-notifications- signalr敲除
一个以上是使用knockout.js等不是。我按照这两个,我不能够满足我的要求。
One of above is using knockout.js and other is not. I have followed both and am not able to meet my requirement.
下面是SignalR集线器类code:
Here is the code from SignalR Hub class:
public void SendEmail(Email email)
{
email.SentBy = WebSecurity.CurrentUserId;
using (NotifyEntities db = new NotifyEntities())
{
UsersContext uc = new UsersContext();
db.Emails.Add(email);
db.SaveChanges();
var ret = new
{
Message = post.Message,
SentBy = post.SentBy,
};
Clients.Caller.sendEmail(ret);
#region add to table which contain user id and email_post_id (I am sending the email to 2 users/receivers)
UsersMessage msgRel = new UsersMessage();
msgRel.EID = email.Id;
msgRel.UID = 2; //user 1
db.UsersMessages.Add(msgRel);
db.SaveChanges();
msgRel = new UsersMessage();
msgRel.EID = email.Id;
msgRel.UID = 5;//user 2
db.UsersMessages.Add(msgRel);
db.SaveChanges();
#endregion
var unread = (from ure in db.Emails.ToList()
join um in db.UsersMessages on ure.Id equals um.EID
where um.UID == WebSecurity.CurrentUserId && um.IsReaded == false
orderby ure.Id descending
select new
{
Message = pst.Message,
SentBy = pst.SentBy,
}).ToArray();
Clients.All.loadUnreadPosts(unread); //this returns unread emails of currently logged in user
Clients.All.loadPosts(ret); // this returns all emails of currently logged in user
}
}
当我不使用knockout.js那么我不能够到店即时通知,但只出现在页面刷新并显示给所有用户,而不是接收者。
When I do not use knockout.js then I am not able to shop instant notification, it only appears on page refresh and it display to all users instead of the receivers.
当我使用knockout.js通知会立即显示出,但同样的问题存在消息/通知显示给所有用户。
When I use knockout.js the notification is showing instantly but the same issue exists that message/notification is displaying to all users.
请帮助我。我不知道如何为特定电子邮件的接收者创建组。
Please help me with this. I dont know how to create group for receivers of particular email.
为了将通知发送给特定的用户只,你想使用小组 SignalR功能。下面是关于如何设置它的一些文件:链接
In order to send the notification to specific users only, you would want to use the Group functionality of SignalR. Here is some documentation on how to set it up: link
您可能会最终在用户登录或一些类似的行动创造每个用户组。
You would probably end up creating a group per user on user login or some similar action.
public async Task UserOnline(string emailAddress)
{
await Groups.Add(this.Context.ConnectionId, emailAddress);
}
那么你将要使用像这样在你的signalr SendEmail
枢纽类。
Clients.Group(emailAddress).loadUnreadPosts(unread);
Clients.Group(emailAddress).loadPosts(ret);
作为一个方面说明,我建议通过文件在我提供的链接去。不应该有任何理由,你需要使用淘汰赛获得signalR适当工作。
As a side note, I recommend going through the documentation at the link I provided. There should be no reason that you need to use knockout to get signalR to work appropriately.
修改
我已经得到了类似之前上述工作的东西(虽然我已经失去了code);但我只是碰到这种文章来一>可能实际上提供了更好的解决方案。
I have gotten something similar to the above working before (although I have lost that code); but I just came across this article that may actually provide a more elegant solution.