C# 服务器和客户端应用程序之间的推送通知机制

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17986907/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 10:58:57  来源:igfitidea点击:

Push Notification mechanism between a server and a client app

c#wcfpush-notification

提问by Tolga Evcimen

I am developping a desktop application using C#, which communicates with a server over a WCF Web Service. It is supposed to be a kind of synchronization application. Meaning that when I make some changes on client app I should immediately update the server too(this is easy using service calls), but also this mechanism should be provided vice versa, a change on server must immediately be applied on several clients. I know I can make polling to my server but this doesn't look neat to me, and also I've heard of duplex services, but I'm not sure if I can use it to implement this mechanism.

我正在使用 C# 开发桌面应用程序,它通过 WCF Web 服务与服务器进行通信。它应该是一种同步应用程序。这意味着当我对客户端应用程序进行一些更改时,我也应该立即更新服务器(这很容易使用服务调用),但也应该提供这种机制,反之亦然,服务器上的更改必须立即应用于多个客户端。我知道我可以对我的服务器进行轮询,但这对我来说并不整洁,而且我也听说过双工服务,但我不确定是否可以使用它来实现这种机制。

I am asking for some suggestions over this issue. Thanks in advance.

我正在就这个问题征求一些建议。提前致谢。

采纳答案by Brice2Paris

You need to implement a publish and subscribe design. In WCF you could use net-tcp protocol to connect in duplex clients and server.

您需要实现发布和订阅设计。在 WCF 中,您可以使用 net-tcp 协议连接双工客户端和服务器。

You could download a quite good implementation on http://www.idesign.net/Downloads/GetDownload/2032

你可以在http://www.idesign.net/Downloads/GetDownload/2032上下载一个很好的实现

And you will find a good article here.

你会在这里找到一篇好文章。

Regards

问候

回答by Serkan Polat

tolga, you can use winforms with signalR, server side can be asp.net or a standalone windows application..

tolga,你可以使用带有signalR的winforms,服务器端可以是asp.net或一个独立的windows应用程序..

回答by marifrahman

PushSharp:A server-side library for sending Push Notifications to clients (the clients are basically mobile devices - but can be custom as well)! You google to get some tutorial on PushSharp use as well.

PushSharp:用于向客户端发送推送通知的服务器端库(客户端基本上是移动设备 - 但也可以自定义)!您也可以通过 google 获取有关 PushSharp 使用的一些教程。

回答by Nitin Suryawanshi

        var webAddr = "https://fcm.googleapis.com/fcm/send";

        var httpWebRequest = (HttpWebRequest)WebRequest.Create(webAddr);
        httpWebRequest.ContentType = "application/json";
        httpWebRequest.Headers.Add("Authorization:key=YOUR_SERVER_KEY");
        httpWebRequest.Method = "POST";

        using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
        {
            string json = "{\"to\": \"/topics/news\",\"notification\": {\"body\": \"New news added in application!\",\"title\":\"" + Your_Notif_Title+ "\",}}";
            streamWriter.Write(json);
            streamWriter.Flush();
        }

        var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
        using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
        {
            result = streamReader.ReadToEnd();
        }