简体   繁体   中英

Best practice for an AJAX call to running C code?

I have an underlying C application driving some pretty intensive hardware. I'm currently designing a web interface for the device, the majority of it's function is to present status information to the user. 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. Current setup:

  1. Javascript from the web status page makes an AJAX call (from setInterval)
  2. PHP file runs, writes to FIFO
  3. C application reads FIFO , does some work based on input (likely blank, meaning just give me the stats), writes back to FIFO
  4. PHP file reads from FIFO , parses, json_encode, echo
  5. Javascript parses JSON and updates page

Here's where you come in... the goal is to take out the PHP middleman. What are the conventions (assuming they exist) for AJAX calls to C programs? Best practice?

Options (as far as I know):

  • Sockets . Have the C program listen on a particular port . Something similar here.
  • 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? Just need some insight here.
  • CGI . Can cgi work with a C program that constantly runs? Or does it load up a C program from scratch when called?
  • 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. In our case we send JSON to and from the C++ backend via websockets.

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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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