windows 在 Web 和桌面应用程序之间进行通信的好方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8643814/
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
Good way of communicating between web and desktop app
提问by John Assymptoth
I want my web app (running in a browser) to, at some point, communicate (bidirectionally) with a desktop app (in Windows), the client has installed in its computer.
我希望我的网络应用程序(在浏览器中运行)在某个时候与桌面应用程序(在 Windows 中)通信(双向),客户端已安装在其计算机中。
What's an elegant and modular and simple way of doing this (with Java and C#)?
这样做的优雅、模块化和简单的方法是什么(使用 Java 和 C#)?
Not much information will be passed between the web app and the desktop app. Just something like "I was here", "Pick this from this point", etc.
Web 应用程序和桌面应用程序之间不会传递太多信息。就像“我在这里”,“从这一点选择这个”之类的东西。
回答by De ruige
I solved that problem by using a database on the network. All communications where made trough the database.
我通过使用网络上的数据库解决了这个问题。通过数据库进行的所有通信。
Website -> DB -> User logged in <- DB <- Desktop
网站 -> DB -> 用户登录 <- DB <- 桌面
However, if no trusted information needs to be shared, you could consider just posting and reading some http headers to a common website, or a simple log file.
但是,如果不需要共享可信信息,您可以考虑将一些 http 标头发布并读取到公共网站或简单的日志文件。
Greetings
你好
回答by keatch
I suggest you to use the backend part of your webapp, assuming that your app is based on some backend services.
我建议您使用 webapp 的后端部分,假设您的应用程序基于某些后端服务。
You have two options:
您有两个选择:
- Your desktop apps use the same services of your web app. You must use a class that mimic a web-browser to give the data (curl, ie). If your web app is based on AJAX push ( APE Serveri.e ) use library that is able to run some javascript
- Use a REST protocol, with a JSON format in your backend services. It's easy to manage and is supported by many client-side languages (java/c#/python....)
- Use a specialized endpoint only for your desktop app,for C#, you can use WCF, that allow you, in one of his forms bidirectional communications. For JAVA, there are WSDL, DWR
- 您的桌面应用程序使用与您的 Web 应用程序相同的服务。您必须使用模拟网络浏览器的类来提供数据(curl,即)。如果您的网络应用程序基于 AJAX 推送(APE 服务器即),请使用能够运行一些 javascript 的库
- 在后端服务中使用 REST 协议和 JSON 格式。它易于管理并且被许多客户端语言支持(java/c#/python....)
- 仅对您的桌面应用程序使用专门的端点,对于 C#,您可以使用 WCF,它允许您以其中一种形式进行双向通信。对于JAVA,有WSDL、DWR
My preferred solution is to decouple the web app in a front-end side and a backend side, that expose the services as REST that are used by the web app via AJAX.
我的首选解决方案是在前端和后端分离 Web 应用程序,将服务公开为 Web 应用程序通过 AJAX 使用的 REST。
If I need true bidirectional communication with other desktop app, I'll create a separate service / endpoint for it. (APE , WCF, ..)
如果我需要与其他桌面应用程序进行真正的双向通信,我将为它创建一个单独的服务/端点。(猿,WCF,..)
回答by Yahia
I see several options to achieve part of what you are asking:
我看到了几个选项来实现您所要求的部分内容:
Besically you expose the relevant parts of your apps (web and desktop) via some sort of API and use that for the communication - for example DB and/or WCF (SOAP/REST/whatever).
Besically 您通过某种 API 公开应用程序的相关部分(Web 和桌面)并将其用于通信 - 例如 DB 和/或 WCF(SOAP/REST/任何)。
BEWARE:
谨防:
If you are after some sort of direct/interactive communication between both apps on the same computer then this won't be possible - at least not without some security related issues.
如果您在同一台计算机上的两个应用程序之间进行某种直接/交互式通信,那么这是不可能的 - 至少在没有一些安全相关问题的情况下是不可能的。
The browser executes your web app in a sandbox which doesn't allow for direct communication between the web app and the desktop... there are technologies which can help circumvent that (ActiveX, signed Java applets, some Flash technology called AIR etc.)... or you could host some webbrowser/control in your desktop app which in turn runs the web app... BUT basically I would recommend against using any such measures...
浏览器在不允许 Web 应用程序和桌面之间直接通信的沙箱中执行您的 Web 应用程序……有一些技术可以帮助规避这种情况(ActiveX、签名的 Java 小程序、一些称为 AIR 的 Flash 技术等) ...或者你可以在你的桌面应用程序中托管一些 webbrowser/control ,它反过来运行 web 应用程序......但基本上我会建议不要使用任何此类措施......
IF you really want to go this route then please describe your environment and goal with much more detail.
如果你真的想走这条路,那么请更详细地描述你的环境和目标。