我在使用 Hangfire 的应用程序中有一些重复的后台作业:
public class NotificationWeeklyHub : Hub
{
public void SendNotificationWeekly()
{
/*Recurring Job
Clients.All.SendNotificationWeekly();
*/
// code....
}
}
所以我想为我的用户发送通知,在这种情况下,我想在应用程序启动时调用我的集线器,那么我如何以这种方式调用我的集线器呢?
PS:我的客户端代码调用集线器:
<script src="~/Scripts/jquery.signalR-2.1.2.min.js"></script>
<script src="~/signalr/hubs"></script>
<script src="~/Scripts/toastr.min.js"></script>
<script>
var notifyWeekly = $.connection.notificationWeekly;
notifyWeekly.client.sendNotificationWeekly = function () {
toastr.success("Message");
};
$.connection.hub.start().done(function() {
notifyWeekly.server.sendNotificationWeekly();
});
</script>
我的目标只是在这些特定时间发送通知(在我的情况下使用 toastr 库)。知道吗?
提前致谢
类似的东西可能会做到:
protected void Application_Start()
{
// get the hub from the globalHost
IHubContext hubContext = GlobalHost.ConnectionManager.GetHubContext<NotificationWeeklyHub>();
// send the message to all clients, make sure that the method is camelCase.
hubContext.Clients.All.sendNotificationWeekly("your message");
}
编辑:
现在关于您的中心代码,看起来您的方法只是调用客户端,并且由于集线器中的任何公共方法都可供客户端使用,因此您可以清理集线器代码并将其更改为如下所示:
public class NotificationWeeklyHub : Hub
{
}