doupian9490 2012-10-10 07:58
浏览 42
已采纳

在线游戏,或者比ajax更快的发送/接收数据

I'm making this as short as I can.

What I've done so far: The game I have will be running with JavaScript using setTimeout or setInterval. It's not flash or anything special. What I have made so far as like a test run (so you can understand better), is pretty much loop Ajax to keep sending requests to a PHP page as fast as it can, which then PHP reads the $_GET in the url from the request, then PHP edits a file called p1.html with the $_GET, which is simply player 1's x and y-axis coordinates. So in player 2's browser, it pretty much did what I said above, and now when it receives the Ajax request, it will receive the coordinates of player 1. So JavaScript does what it does, and moves player 1 on player 2's browser. That's what I already made myself, and I tested it and yes it works and yes it lasts forever when I fixed all the bugs and stuff.

Saying that, that's what I've made so far, but this isn't fast enough if I wanted like a online fighting game or a real time side scrolling game. So that's what I need help with. I know a lot of stuff on w3schools.com, but I just don't know how to get this job done. That's probably it. I just need browser 1, to get data to browser 2, something like this "1,100,200" or a little longer actually, and have browser 2 read that data as a variable in JavaScript (anything like x="received data";), and that's just it. JavaScript will do the rest.

I'm sure I can program everything I need myself, but for sending data from point A to point B like 50 times a second, I just don't know anything about it. Not even a name. And last, urls and examples would be very nice (to be clearer). Especially if it's already on w3schools.com (with examples) and I just missed it.

  • 写回答

6条回答 默认 最新

  • 普通网友 2012-10-10 08:00
    关注

    The answer is simple : Use websockets. They allow the immediate push of data in both directions for seemingly instantaneous interactions. They eliminate the need of pulling data from the client which is slow and heavy (including for the server). Note that's the solution used in my favorite game for notification push.

    You might be interested by this introduction on websockets on PHP. But note that PHP really isn't the most efficient solution for long connections handling and real time games (you might prefer Go, node.js, java, etc.).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(5条)

报告相同问题?

悬赏问题

  • ¥30 自适应 LMS 算法实现 FIR 最佳维纳滤波器matlab方案
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥15 Python3.5 相关代码写作
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像