一个好的起点是here,请记住,检查日期是否匹配需要某种周期性任务(如果你想将所有内容保存在同一个网络应用程序中,你可以查看HangFire)
一种简单的方法如下所示:
集线器
Kepp 说,如果您有多个服务器或工作进程,您可能需要将 SignalR 连接和用户名之间的映射存储在其他地方,请查看 here。
public class ReminderHub : Hub
{
public Dictionary<string,string> _conn = new Dictionary<string,string>();
public void Store(string username, DateTime date)
{
// Store into the database
// ....
// ...
// Store the realation between the connection and the username
_conn.Add(username,Context.ConnectionId);
}
public void Notify(string username)
{
// notify method is defined in the client (js)
Clients.User(_conn[username]).notify(username);
}
}
网络客户端
省略日期格式等细节以保持答案简短
var hub = $.connection.reminderHub;
hub.client.notify = function (username) {
alert(username)
};
$.connection.hub.start().done(function () {
// Wire up save reminder option.
$('#save').click(function () {
hub.server.Store($('#username').val(), $('#date').val());
});
});
任务
对于定期任务,您有多个选项,HangFire 任务、窗口服务或事件、作为计划任务运行的简单控制台应用程序。
我假设这是一个控制台应用程序。
您将需要.Net SignalR Client,查看客户端的正确设置。
var hubConnection = new HubConnection("**YOUR URL**);
await hubConnection.Start();
IHubProxy proxy = hubConnection.CreateHubProxy("ReminderHub");
// QUERY THE DATABASE Check if there's any user to notify
for(var username in UsersToNotify){
proxy.Invoke("Notify", username);
}
请记住,对我来说有很多改进,此代码只是简单的方法。