Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Online gaming, or something faster then ajax for sending/receiving data

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.

like image 538
Hybrilynx Avatar asked Oct 10 '12 07:10

Hybrilynx


1 Answers

you can use the websocket

The advantage with WebSockets (over AJAX) is basically that there's less HTTP overhead. Once the connection has been established, all future message passing is over a socket rather than new HTTP request/response calls. So, you'd assume that WebSockets can send and receive much more messages per unit time. Turns out that that's true. But there's a very bitter reality once you add latency into the mix.

WebSockets are roughly 10-20% faster than AJAX

source

what happen when we use ajax with php is

  1. it open a new connection to apache server
  2. than apache looks for php script and launch it
  3. now the php script will connect to server to query and it will return result .

but what the websocket does is that eliminate 2 connecting processes and will just send a message to the server. server is already be connected to the sql server

Another benefit is the that the connection between the client and the server stays open, the server can send messages to the client. in ajax you need to cal each time

like image 120
NullPoiиteя Avatar answered Nov 03 '22 09:11

NullPoiиteя