AJAX request not working as it should

0 favourites
  • 9 posts
From the Asset Store
2D fighting template based in the game that defined the fighting games genre.
  • Hi.

    I am working on a multiplayer game where both player and opponent is using a request AJAX command to get data from the database, can for example be that it gets info if both players are ready to start a new game and so on... So what happens is after a few games some of the requests (never all of them at the same time) seems to stop and the game stops because its waiting to receive the data before continuing. Sometimes after waiting a while 30 sec - 1 min the request resumes and the game continues, while other times it is stuck forever.

    I am using the request command on every X amount of sec and tried to have it on its own and inside a every tick command, and both are producing the same results.

    Sometimes I am able to play a game for a couple of min without anything happening, but sooner or later it always happens..

    I know it is the request command as I have used "On Error" and whenever there is an error with the request I have added a custom text that shows up always when the game stops.

    I wonder if anyone of you are familiar with AJAX request and have had this issue before, and maybe knows some suggestions I could try to fix it.

    Thanks a lot.

    Regards,

    Edgar

  • Sending AJAX requests on every tick is not a good idea, your server will probably not be able to respond that fast.

    Have you tried checking browser console log? When there are any issues with AJAX, you can usually find an error message in console log.

  • Sending AJAX requests on every tick is not a good idea, your server will probably not be able to respond that fast.

    Have you tried checking browser console log? When there are any issues with AJAX, you can usually find an error message in console log.

    Thanks for the response dop2000.

    I didn't find any error message in the console.

    Would you recommend another way other than placing it every tick? I was also thinking that since I have about 6 - 7 requests running simultaneously the reason they stop is because the server cannot respond to so many requests at one time. But for example when the player makes a move the opponent needs to have a request that checks for the message from the player as often as possible or else the game will be delayed if I have a request on lets say every 5 seconds to give the server time to respond. I also thought it could be poor connection on my side, but the network is fine and still some of the requests are able to get through.

    I would appreciate any ideas you might have. Perhaps a POST command would work more sufficient? Eventually I have to change all the requests to POST because I have to post the username of the players to make sure they are receiving data only for their hosted game, to not interfere or accidentally update other games.

    Thanks.

  • Would anyone else have any other ideas?

    Thanks.

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • Have you find the error message in the console log?

  • Have you find the error message in the console log?

    I have not seen any errors in the console log. Where exactly can i see it?

    Thanks.

  • When previewing the game, or running an exported game in browser, press F12 and select Console tab. This is the first place you need to look when there are issues with AJAX.

  • When previewing the game, or running an exported game in browser, press F12 and select Console tab. This is the first place you need to look when there are issues with AJAX.

    Thanks, got it now.

    So I am getting this error message:

    Access to XMLHttpRequest at 'https://gem24.cc/get_host_message.php' from origin 'https://preview.construct.net' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

    I have this code on top of all my PHP scripts and it has always worked.

    header("Access-Control-Allow-Origin: *");

    Do you know if i need to add something else in the script? Do I perhaps need to request data over HTTP instead of HTTPS?

    Thanks.

  • Sorry, I can't help you with this. But this is a very common error with AJAX, I've seen lots of posts here about it. Try searching the forum.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)