我創建了一個通知系統,每次有新通知時都會通知用戶。支持Ajax的WCF服務 - 實現觀察者設計模式
當前在我下面的客戶端腳本中,我每秒都會調用Web服務調用來爲服務器查詢新通知。每當有新的通知時,我將如何能夠讓支持Ajax的WCF通知客戶端?
有沒有人有任何資源,建議或教程來實現觀察者設計模式使用啓用Ajax的WCF? *注意:使用Observer模式可能不是實現這一點的最佳方式,任何關於最佳模式的建議(可能是我當前的實現)都非常感謝。
客戶方:
$(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());
}
我相信我目前僅限於使用WCF進行此項目,但感謝您的選擇。我一定會考慮這個。 – jocey 2012-07-22 19:11:26