简体   繁体   English

支持Ajax的WCF服务 - 实现Observer设计模式

[英]Ajax-Enabled WCF Service - implementing Observer Design Pattern

I created a Notifications system that would notify the user each time there are new notifications. 我创建了一个Notifications系统,每次有新通知时都会通知用户。

Currently in my clientside script below, I am calling the web service call every second to ping the server for new notifications. 目前在我的客户端脚本中,我每秒都调用Web服务调用来ping服务器以获取新通知。 How would I be able to instead have the Ajax-enabled WCF notify the client each time there is a new notification? 如果有新的通知,我怎样才能让启用Ajax的WCF通知客户端?

Does anyone have any resources, suggestions, or tutorials to implement an Observer Design pattern using an Ajax-enabled WCF? 有没有人有任何资源,建议或教程来使用支持Ajax的WCF实现Observer设计模式?

*Note: using Observer pattern might not be the best way to implement this, any advice on the best pattern (could be my current implementation) is much appreciated. *注意:使用Observer模式可能不是实现此方法的最佳方式,对最佳模式(可能是我当前的实现)的任何建议都非常感谢。

Clientside: 客户端:

$(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);
    }

});}

Notification Service 通知服务

[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());
    }

Are you tied to using WCF? 你喜欢使用WCF吗? SignalR might be more appropriate for this kind of push notifications. SignalR可能更适合这种推送通知。

Update: Just to clarify; 更新:只是为了澄清; SignalR abstracts a mechanism for the server to push data to a number of clients, one of which is JavaScript. SignalR抽象了一种服务器将数据推送到多个客户端的机制,其中一个客户端是JavaScript。 On the client you can hook event handlers that deal with notifications as they are sent by your notification manager... 在客户端上,您可以挂钩处理通知的事件处理程序, 因为它们是由通知管理器发送的...

Cheers, Dean 干杯,迪恩

As far as my understanding of the web model goes, there's no way the server(WCF service) can notify the client(AJAX application) without a client requesting for notification. 据我对Web模型的理解,服务器(WCF服务)无法在没有客户端请求通知的情况下通知客户端(AJAX应用程序)。 That seems to be the core of the whole Request-Response system between a Client and a Server. 这似乎是客户端和服务器之间整个请求 - 响应系统的核心。

I understand that you need this to make your application more efficient and there are a few suggestions that might help: 我知道您需要这样才能使您的应用程序更有效率,并且有一些建议可能有所帮助:

  • HTTP polling duplex WCF channel - This doesn't exactly give what you want, as the client would still be polling for changes(notifications) to the client. HTTP轮询双工WCF通道 - 这并不能完全满足您的需求,因为客户端仍会轮询客户端的更改(通知)。 But still, it is tailor-made for what you would like in your application. 但是,它仍然是为您的应用程序所需的量身定制的。
  • If you cannot /don't want to use polling based WCF and stick to your current mechanism, I'd suggest you to change the service from a POST to GET as apparently, you wont have to send any complex data while fetching notifications. 如果您不能/不想使用基于轮询的WCF并坚持当前的机制,我建议您将服务从POST更改为GET因为显然,您不必在获取通知时发送任何复杂数据。 A GET is a bit quicker and than a POST in an AJAX scenario. 在AJAX场景中, GETPOST更快。

This will be possible in the new release of WCF (4.5). 这可以在新版本的WCF(4.5)中实现。 You can use Websockets for two way communication with HTTP clients. 您可以使用Websockets与HTTP客户端进行双向通信。 You won't ne using AJAX then, but javascript with websockets. 你不会再使用AJAX,而是使用带有websockets的javascript。 Check out the following link for more info: http://www.codeproject.com/Articles/338789/What-s-new-in-WCF-4-5-WebSocket-support-Part-1-of 有关详细信息,请查看以下链接: http//www.codeproject.com/Articles/338789/What-s-new-in-WCF-4-5-WebSocket-support-Part-1-of

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM