directional with server program?

I want to write a web application to play chess against the engine Crafty. I'm not new to PHP and javascript, but must learn how to interact with a server process : how can a web application and/or (jQuery) ajax interact bi-directionally with a (linux) program running on the server?

At this moment i am developing on (Apache) local host. Crafty is installed on my Ubuntu PC. This well-known chess engine has no GUI, it runs in terminal by the command

$ /usr/games/crafty

and so you can play chess against it and even see it's calculations :

狡猾的终端

I can make Crafty run by PHP, using the functions proc_open() or exec(), and most documentation i found states that the output stream should be a file .. But i think i don't want such setup, because then the webpage should be constanty polling that file (eg. by ajax) to see if some new data was appended, right?

How can Crafty talk to the web page directly, saying "i have calculated another variation" or "i have decided a move" etc, then display this info on the web page and let the user give some counter move, just like in terminal. Isn't it possible to use some session / stream / listener?

I have no clue at all, can anybody point me in a right direction?


I recommend you make use of fifos and the & operator - here is why:

  • You do not want to start crafty on every PHP request, you want to start it only once per game
  • You don't want to have crafty end at the end of your Request
  • Your move-requests will want to interact with this allready running instance.
  • So what I would do is something like:

  • Prepare a pair of FIFOs using mkfifo - you can do this from PHP or from the shell
  • On game start, run something like /usr/games/crafty <stdin.fifo >stdout.fifo 2>stderr.fifo &
  • For your moves, make an AJAX PHP request write to stdin.fifo
  • For the server moves do long polling with AJAX, on the server side opening stdin.fifo , then stream_select()
  • 链接地址: http://www.djcxy.com/p/60700.html

    上一篇: Web服务器如何填充$

    下一篇: 与服务器程序定向?