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.
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With