我创建了一个通知系统,每当有新的通知时会通知用户。
目前,在下面的客户端脚本中,我每秒调用一次Web服务来检测是否有新通知。如何使用启用Ajax的WCF,让它在每次有新通知时通知客户端?
是否有任何资源、建议或教程,可以实现使用启用Ajax的WCF实现观察者设计模式?
*注意:使用观察者模式可能不是实现此功能的最佳方式,非常感谢关于最佳模式(可能是我的当前实现)的任何建议。
客户端:
通知服务
目前,在下面的客户端脚本中,我每秒调用一次Web服务来检测是否有新通知。如何使用启用Ajax的WCF,让它在每次有新通知时通知客户端?
是否有任何资源、建议或教程,可以实现使用启用Ajax的WCF实现观察者设计模式?
*注意:使用观察者模式可能不是实现此功能的最佳方式,非常感谢关于最佳模式(可能是我的当前实现)的任何建议。
客户端:
$(document).ready(function () {
self.setInterval("getNewNotificationsCount()", 1000);
});
function getNewNotificationsCount() {
$.ajax({
type: "POST",
url: site_root + "services/NotificationService.svc/json/GetNewNotificationsCount",
contentType: "application/json; charset=utf-8",
data: {},
dataType: "json",
error: function (request, error, u) {
alert('error: ' + error);
},
success: function (result, status) {
$('#hip_badge').text(result.d);
}
});}
通知服务
[ServiceContract(Namespace = "")]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
[ServiceBehavior(IncludeExceptionDetailInFaults = true)]
public class NotificationService {
{
[OperationContract]
[WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json)]
public int GetNewNotificationsCount()
{
NotificationManager nm = new NotificationManager();
return nm.getNewNotifications(GetUserName());
}