简体   繁体   English

AJAX调用来运行C代码的最佳实践?

[英]Best practice for an AJAX call to running C code?

I have an underlying C application driving some pretty intensive hardware. 我有一个底层的C应用程序,用于驱动一些非常密集的硬件。 I'm currently designing a web interface for the device, the majority of it's function is to present status information to the user. 我目前正在为设备设计一个Web界面,其大部分功能是向用户显示状态信息。 So every second or so, update fields on the web page with pertinent information (temperature, framerate, yada yada...) The device runs linux and has apache. 因此,每隔一秒钟左右,用相关信息(温度,帧速率,yada yada ...)更新网页上的字段。该设备运行linux并且具有apache。 Current setup: 当前设置:

  1. Javascript from the web status page makes an AJAX call (from setInterval) Web状态页面中的Java语言进行AJAX调用(来自setInterval)
  2. PHP file runs, writes to FIFO PHP文件运行,写入FIFO
  3. C application reads FIFO , does some work based on input (likely blank, meaning just give me the stats), writes back to FIFO C应用程序读取FIFO ,根据输入做一些工作(可能为空,意味着只给我统计信息),写回FIFO
  4. PHP file reads from FIFO , parses, json_encode, echo PHP文件从FIFO读取,解析,json_encode,echo
  5. Javascript parses JSON and updates page Javascript解析JSON并更新页面

Here's where you come in... the goal is to take out the PHP middleman. 这是您进入的地方...目标是淘汰PHP中间人。 What are the conventions (assuming they exist) for AJAX calls to C programs? AJAX调用C程序有哪些约定(假设它们存在)? Best practice? 最佳实践?

Options (as far as I know): 选项(据我所知):

  • Sockets . 插座 Have the C program listen on a particular port . 让C程序监听特定的端口 Something similar here. 这里有些相似。
  • Node.js. Node.js的 I'm not familiar with this, I've never used it. 我对此不熟悉,我从未使用过。 But my brief exposure makes me think this may be a great use for it? 但是我的简短接触使我认为这可能是一个很好的用途? As I understand it... it's "javascript on the server side", but it's actually C code? 据我了解...它是“服务器端的javascript”,但实际上是C代码? Just need some insight here. 这里只需要一些见识。
  • CGI . CGI Can cgi work with a C program that constantly runs? cgi可以与持续运行的C程序一起使用吗? Or does it load up a C program from scratch when called? 还是在调用时从头开始加载C程序?
  • HTML5 WebSockets ? HTML5 WebSockets

Any insight appreciated. 任何见解表示赞赏。 Of course, the best answer is quite possibly not even listed above. 当然,最好的答案甚至可能没有在上面列出。 That's just the extent of my knowledge in this area. 那只是我在这方面知识的程度。 Thanks! 谢谢!

We have used the C library libwebsockets ( http://libwebsockets.org/ ) to communicate between JavaScript and a C++ application. 我们已经使用C库libwebsockets( http://libwebsockets.org/ )在JavaScript和C ++应用程序之间进行通信。 In our case we send JSON to and from the C++ backend via websockets. 在我们的例子中,我们通过websockets向C ++后端发送JSON。

In your case you could take the same data that you currently send via Ajax and send it to the C program over the websocket. 在您的情况下,您可以获取当前通过Ajax发送的相同数据,然后通过websocket将其发送到C程序。 The C program can then generate the same JSON that the PHP program generates and send that back to the JavaScript, again via the websocket. 然后,C程序可以生成与PHP程序生成的JSON相同的JSON,然后再次通过websocket将其发送回JavaScript。

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

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