doushan2224 2013-03-23 15:19
浏览 38

AJAX / PHP异步计算

I am currently planning an Achievements system for my project. Achievements will be unlocked when the user actually visits the Achievements page, and any uncompleted ones will have their progress shown.

It may take some time to calculate some of the achievements' progress. I came up with this idea, but I have no idea how to go about implementing it, or if it's even possible:

  • JavaScript requests achievements data via AJAX
  • PHP starts calculating achievement progress
  • For each achievement, a line is written to the output stream, of the form ID:progress
  • JavaScript receives each line and shows the progress for the given achievement

How would I go about having AJAX read the responseText line-by-line as it arrives, rather than all at once at the end?


Additionally, if possible, I'd like for PHP to be threaded like so:

  • For each achievement:
    • If there are more than N "threads", wait for one to finish
    • Fork off a thread to calculate the current achievement and echo the result line

Is such a thing possible in PHP?


Or am I just going about this completely the wrong way? Should I just send an AJAX request for each group of achievements? Or one per achievement? What would you suggest as an alternative?

  • 写回答

1条回答 默认 最新

  • doushifen4060 2013-03-23 15:39
    关注

    You could achieve this in modern ES5 browsers by using a combination of XHR2 Progress Events on the client and HTTP Chunked Transfer Encoding on the server - this Stack Overflow post goes into more detail.

    Personally, I would say this approach is a little over-engineered. If it's going to take less than a couple of seconds to fetch the player achievements the first time (presuming you will cache it for subsequent requests in a given time period) then I would just make the player wait whilst they download and look to optimise this once I had enough users to warrant it :)

    评论

报告相同问题?

悬赏问题

  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)