How do I make sure AJAX.LastData corresponds to the correct request?

0 favourites
  • 6 posts
From the Asset Store
2D Tile Set
$2.99 USD
Tile map for your 2d platformer games, custom sets available on request
  • I'm working on a multiplayer game. When a player logs in the server gets a message and sends an AJAX request to my webserver. When the request is completed, the server loads ajax.lastData and return the username to the player.

    I'm wondering if two players were to login at roughly the same time, might it be possible that the server ends up swapping the players usernames?

    Scenario:

    1. Player A and B login and send message to server
    2. Server sends AJAX request for Player A's username
    3. Server sends AJAX request for Player B's username
    4. AJAX request for Player B's username ends up getting received first
    5. Server sets Player A's username to "Player B" (mistaking Player A for Player B)
    6. AJAX request for Player A's username is received second
    7. Server sets Player B's username to "Player A" (mistaking Player B for Player A)

    Thanks,

    bad

  • Queue them, then no problem !

  • Queue them, then no problem !

    Sounds great in theory. Do you have any recommendations for how I could I go about doing that?

    Is there a potential that this could cause a bottleneck?

  • Has it ever happened in tests?

    It’s probably the type of thing that’s not worth handling unless it actually happens.

    I don’t mess with networking, much less server side stuff. An Ajax request shouldn’t mix stuff like that. I mean you could probably make server code that would do that purposely if you wanted.

    Best I understand it, for the Ajax request to work the server will receive the request with where it came from so it can send a result back. It’s not possible to mix it up accidentally.

  • Hey R0J0hound, thanks for the reply. It hasn't happened in tests - it's just something I thought about.

    It definitely seems like it could be an issue down the road if the game ever achieves 100+ players or so, where they're all talking to the server at the same time, and the servers making the requests for them.

    In my example, if the AJAX request uses the tag "getUsername", the corresponding condition "AJAX on "getUsername" completed" may not actually be tied to the initial request, but rather the first incoming response with the tag "getUsername".

    I'm worried that because the time it takes to send and receive a response varies for every request that goes out, it might mix things up (eventually).

    I noticed something called an "X-Request ID" in the response headers, I'm wondering if the AJAX plugin takes this x-request ID into account in order to validate the request is a match once "on getUsername completed" fires.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • For anyone interested, I guess one way to be sure is to use custom tags with the identifier attached. Its messy and I'm not even sure if it's necessary but it's a workaround:

    Call getUsername(player.account)

    + function: On "getUsername"

    -> ajax: Send AJAX request with tag "getUsername_" & function.Param(0))

    + ajax: On any completed

    -> json: Load content from ajax.LastData

    //tokenize the tag to get the tag name

    ----+ System: tokenat(ajax.Tag,0,"_") = "getUsername"

    //tokenize the tag again to get the account name

    --------+ player: account = tokenat(ajax.Tag,1,"_")

    ---------> player: Set account to json.At("data.username")

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