处理 XMLHttpRequest 来调用外部应用程序
我需要一种简单的方法来使用 XMLHttpRequest
作为 Web 客户端访问嵌入式设备中的应用程序的方式。我在尝试弄清楚如何制作轻薄的东西来处理传入 Web 服务器的 XMLHttpRequests 并将其转换为应用程序调用时感到困惑。
情况:
- 使用 Ajax(特别是 ExtJS)的 Web 客户端需要与现有的嵌入式应用程序异步发送和接收。这不仅仅是拥有一个胖客户端/瘦服务器,客户端还需要对应用程序状态进行后台检查。
- 应用程序可以公开套接字接口,其中包含一组已知的命令、事件和配置值。配置可能会以 XML 形式传输,因为它来自 SQLite 数据库。
- 在客户端和应用程序之间是一个 lighttpd Web 服务器,运行着某种东西来以某种方式处理翻译。这个某事就是问题所在。
我认为我想要的:
- Lighttpd 可以使用 FastCGI 将所有
XMLHttpRequest
路由到外部进程。此过程将理解 HTML/XML,并在其与应用程序语言之间进行翻译。它将具有自定义逻辑来模拟向客户端推送通知(接收 XMLHttpRequest,在下一个通知可用之前不响应)。 - C/C++。我真的很想避免在嵌入式设备上安装 Java/PHP/Perl。所以我需要更多低层次的理解。
我该怎么做?
- 是否有好的 C++ 库来解释 CGI 标头和 HTML,以便我不必进行任何语法处理,我可以只处理请求/响应内容?
- 在处理
XMLHttpRequest
和 CGI 接口时,是否有关于服务器端到底发生了什么的好的参考? - 是否有任何软件包已经完成了大部分工作,或者我必须从头开始构建非 HTTP/CGI 的东西?
I need a simple way to use XMLHttpRequest
as a way for a web client to access applications in an embedded device. I'm getting confused trying to figure out how to make something thin and light that handles the XMLHttpRequests
coming to the web server and can translate those to application calls.
The situation:
- The web client using Ajax (ExtJS specifically) needs to send and receive asynchronously to an existing embedded application. This isn't just to have a thick client/thin server, the client needs to run background checking on the application status.
- The application can expose a socket interface, with a known set of commands, events, and configuration values. Configuration could probably be transmitted as XML since it comes from a SQLite database.
- In between the client and app is a lighttpd web server running something that somehow handles the translation. This something is the problem.
What I think I want:
- Lighttpd can use FastCGI to route all
XMLHttpRequest
to an external process. This process will understand HTML/XML, and translate between that and the application's language. It will have custom logic to simulate pushing notifications to the client (receive XMLHttpRequest, don't respond until the next notification is available). - C/C++. I'd really like to avoid installing
Java/PHP/Perl
on an embedded device. So I'll need more low level understanding.
How do I do this?
- Are there good C++ libraries for interpreting the CGI headers and HTML so that I don't have to do any syntax processing, I can just deal with the request/response contents?
- Are there any good references to exactly what goes on, server side, when handling the
XMLHttpRequest
and CGI interfaces? - Is there any package that does most of this job already, or will I have to build the non-HTTP/CGI stuff from scratch?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果我理解正确的话,我解决这个问题的方法将是 3 层(不要太在意我们都听说过的 3 层流行语):
通过这种方式,您可以利用您已经熟悉的所有技术。第 1 层和第 2 层之间的 Ajax 并不是什么新鲜事,并且在第 2 层和第 3 层之间使用套接字。
If I understand correctly, the way I approach this problem would be a 3-tier (Don't get hang up so much on the 3-tier buzz words that we all have heard about):
In this way, you can leverage all technologies that you're already familiar with. Ajax between tier 1 and 2 is nothing new, and use socket between 2 and 3.
我并不是说你不知道socket。我只是提出了一种方法来描述我听到很多单词的问题:XML/HTML/Ajax/XmlHttpRequest/Java/PHP/Perl/C++/CGI 等等,并提供一种简化为更小、更好理解的方法问题。让我澄清一下:
如果您想最终从嵌入式设备检索数据并在浏览器上呈现,然后让浏览器向 Web 服务器发出请求,Web 服务器使用套接字与嵌入式设备通信。数据如何在浏览器和服务器之间传递,这就是普通的 HTTP,不多也不少。 Web 服务器和嵌入式设备之间的情况相同,只是套接字而不是 HTTP。
因此,如果你只解决一个简单的问题,比如将 2 个数字相加。除此之外,这 2 个输入数字将被传递到 Web 服务器,然后 Web 服务器传递到嵌入式设备,在嵌入式设备中执行加法。结果被传回 Web 服务器,再传回浏览器进行渲染。如果您能做到这一点,您就可以让数据流向您想要的任何地方。
如何解析数据取决于您如何设计数据结构,该数据结构可能包括包装有效负载的容器。
“...无论 HTTP 传入服务器的可用信息位,并生成正确的 HTTP 响应”
...但这与您在服务器上处理 HTTP 请求的方式没有任何不同您的服务器端语言。
...如何用C/C++实现后端进程,而不是安装PHP之类的包
如果嵌入式设备是用C/C++编程的,你需要知道如何在C/C++中进行socket编程C/C++。在你的网络服务器上,你还必须知道如何进行套接字编程,除了使用服务器端语言。
希望这有帮助。
I did not mean that you did not know socket. I just proposed a way to take a desc of a problem where I hear a lots of words: XML/HTML/Ajax/XmlHttpRequest/Java/PHP/Perl/C++/CGI and more and offer a way to simplify into smaller, better understood problem. Let me clarify:
If you want to ultimately retrieve data from the embedded devices and render on the browsers, then have the browsers making a request to the web server, the web server uses socket to talk to the embedded device. How the data is passed between browser and server, that's normal HTTP, no more, no less. Same thing between web server and embedded device, except socket instead of HTTP.
So if just you take a simple problem, like doing an addition of 2 numbers. Except these 2 input numbers would get passed to the web server, and then the web server passes to the embedded device, where the addition is carried out. The result gets passed back to the web server, back to the browser for rendering. If you can do that much, you can already make the data flow everywhere you want to.
How to parse the data depends on how you design the structure of the data that might include container which wraps around a payload.
"... whatever HTTP is coming to the server into usable bits of information, and generate the proper HTTP response"
...but that's not any different than how you handle the HTTP request on the server using your server-side language.
...how to implement a backend process in C/C++, instead of installing a package like PHP
If the embedded device is programmed in C/C++, you are required to know how to do socket programming in C/C++. On your web server, you also have to know how to socket programming, except that will be in that server-side language.
Hope this helps.