简体   繁体   English

如何在Web App和C Sharp之间进行通信

[英]How to Communicate Between Web App and C Sharp

I'm attempting to make a Web app that needs to communicate to a program written in C Sharp. 我正在尝试制作一个需要与使用C Sharp编写的程序进行通信的Web应用程序。 But I can't find a good form of communication. 但是我找不到一种好的交流方式。 What I need is if a user clicks something on the Web app, it will notify the C Sharp program. 我需要的是,如果用户单击Web应用程序上的某些内容,它将通知C Sharp程序。 Also, if an event happens on the C Sharp program, it needs to alert the Web app. 另外,如果C Sharp程序上发生了事件,则需要提醒Web应用程序。 Both of these are going to be running on the same machine. 这两个都将在同一台计算机上运行。

Right now I'm mainly focusing on the C Sharp program just periodically "asking" what the status of the Web app is. 现在,我主要关注C Sharp程序,只是定期“询问” Web应用程序的状态。

I've tried using POST requests to the Web app and I've had a bit of success with this but I don't know how to essentially store and update a "status" on the Web App. 我已经尝试过使用对Web应用程序的POST请求,并且在此方面取得了一些成功,但是我不知道如何从本质上在Web应用程序上存储和更新“状态”。 For example, C Sharp program sends a POST/GET request asking for the status, the Web app responds with "nothing has changed" or some sort of status code. 例如,C Sharp程序发送POST / GET请求以询问状态,Web应用程序以“什么都没有改变”或某种状态代码来响应。 I don't know how to keep track of that status. 我不知道该如何保持状态。

I've attempted using Web Sockets but I don't think it is going to be possible on the C Sharp side. 我曾尝试使用Web套接字,但我认为C Sharp方面不可能实现。 However, I'm definitely open to suggestions on how this might work. 但是,我绝对愿意就此如何工作提出建议。

I've looked into using the ReST architectural style but I'm having a hard time understanding how I would implement it. 我已经研究过使用ReST架构风格,但是很难理解如何实现它。 I'm using mainly AJAX on an apache server and most of the ReST examples I saw used IIS. 我主要在apache服务器上使用AJAX,而我看到的大多数ReST示例都使用IIS。

One way I've been successful with this is a horrible workaround. 我成功实现此目标的一种方法是一种可怕的解决方法。 I use 3 files to store contents, status of Web app, and status of C Sharp program. 我使用3个文件来存储内容,Web应用程序的状态和C Sharp程序的状态。 But this requires me constantly fetching files, reading them, writing a new one, and uploading it. 但这需要我不断地获取文件,读取文件,编写新文件并上载文件。

Sorry if this question is poorly formed, I'm obviously new to a lot of this and this is my first SO post. 抱歉,如果这个问题的形成方式很差,那么我显然是新手,这是我的第一篇SO帖子。 Also, I'd include example code but I'm posting this from my tablet so it's not accessible right now. 另外,我将提供示例代码,但我将从平板电脑上发布此代码,因此现在无法访问。

If they are on the same machine, you can use 'pipes' (Unix), local sockets or file handlers. 如果它们在同一台计算机上,则可以使用“管道”(Unix),本地套接字或文件处理程序。

These are all types of IO objects both applications can 'listen' to without exposing themselves to the network and without blocking while they are 'waiting' for data.. 这些是所有类型的IO对象,两个应用程序都可以“监听”这些IO对象,而不会暴露给网络,并且在“等待”数据时不会阻塞。

... But this will limit your scalability. ...但这会限制您的可伸缩性。

Another option is to use a Pub/Sub service such as Redis. 另一种选择是使用发布/订阅服务,例如Redis。 This is a better option than websockets, because you can have multiple C# apps listening to multiple web apps on a centralized data source. 这是比websockets更好的选择,因为您可以让多个C#应用程序监听集中式数据源上的多个Web应用程序。

It uses the same IO concept (using sockets) behind an abstraction layer coupled with a database - it's very effective. 它在与数据库耦合的抽象层后面使用相同的IO概念(使用套接字)–非常有效。

Good luck! 祝好运!

I implemented something similar to this. 我实现了类似的东西。 I needed a desktop application to listen for api calls from the browser. 我需要一个桌面应用程序来侦听来自浏览器的api调用。 I ultimately decided to implement a "web connector" which can either be created as part of the application OR installed as a service. 我最终决定实现一个“ Web连接器”,该连接器可以作为应用程序的一部分创建,也可以作为服务安装。

Here is a good example: https://msdn.microsoft.com/en-us/library/system.net.sockets.tcplistener(v=vs.110).aspx 这是一个很好的示例: https : //msdn.microsoft.com/zh-cn/library/system.net.sockets.tcplistener(v=vs.110).aspx

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

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