duanlinpi0265 2012-11-27 12:07
浏览 128

Web应用程序与服务器程序进行双向交互?

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 :

crafty in terminal

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?

  • 写回答

1条回答 默认 最新

  • duanhua5523 2012-11-27 12:20
    关注

    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()
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题