概述:,我想为用户输入创建一个带有警报通知的余数。我有一个视图页面与用户名和日期输入。如果提交成功,则应将其存储在数据库中。使用signalR,如果客户端时间(机器时间)和用户输入时间(服务器时间)匹配,我希望触发提醒。它应该提供一个弹出窗口来显示带有用户名和提醒时间的通知。
解释:
Homepage
Username: textbox
Reminder date and time: textbox
Save reminder: button用户单击“保存提醒”,然后将时间保存在数据库中。当当前时间与使用信号R的提醒日期和时间相对应时,显示带有用户名的弹出窗口。
(要检查输出,请在两个不同的浏览器中打开localhost url,并查看弹出窗口是否同时打开。)
发布于 2015-08-01 14:35:51
一个很好的起点是这里,请记住,检查日期匹配是否需要某种定期任务(如果您想将所有内容保存在同一个web应用程序中,可以查看HangFire)。
一种简单的方法如下所示:
集线器
Kepp在中间表示,如果您有多个服务器或工作流程,您可能必须存储SignalR连接和用户名之间的映射,其他地方看上去像这里。
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客户端,查看一下客户端的正确设置。
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);
}请记住,我有很多改进--这段代码只是简单的方法。
https://stackoverflow.com/questions/31761067
复制相似问题