Page 1 of 1

Too much packets

Posted: 29 Oct 2018, 09:32
by OhMyOhmit
Hey guys
I'm still working with a movement, I got a great progress, but now I need to solve this problem: whenewer player pushes W button to move forward, there is boolean changing, which triggers GameManager to send a packet to server. The problem is, even if I tap on a button very fast, it fastly sends 3-5 packets, which is too much because I keep a List of every packet sent by client and as server can't work with such a big queue, this List on a client is becoming bigger and bigger every second, which isn't good because I start to have some sort of lag between client and server.
Here's code:

PlayerController:

Code: Select all

void Update()
    {
        if (Input.GetAxis("Horizontal") > 0)
        {
            //...
            //MOVE
            //...
            MovementDirty = true;
        }
    }


GameManager:

Code: Select all

void FixedUpdate()
    {
   if (sfs != null)
        {
      sfs.ProcessEvents();
         
      if (localPlayer != null && localPlayerController != null && localPlayerController.MovementDirty)
                {
                      ISFSObject info = new SFSObject();
                   //info.PutSOMEINFOHERE();
                   ln.AddMessage(info);
         localPlayerController.MovementDirty = false;
                   Debug.Log("Added packet to a list");
            }
            if (ln.messages.Count > 0)
            {
               //HERE I SEND EVERY PACKET TO A SERVER, STARTING FROM A FIRST ONE
                ISFSObject info = ln.messages[0].obj;
                info.PutInt("id", ln.messages[0].num);
                sfs.Send(new ExtensionRequest("MovePlayer", info, sfs.LastJoinedRoom));
                Debug.Log("Sent");
            }
            messages.GetComponent<Text>().text = "Messages: " + ln.messages.Count;
      }
   }

When I get a response from server, I delete a message with specific id from a list.
Sometimes (often) it does even send a same message 2 or 3 times.

So the question is how to limit the amount of this packets being sent to server. Maybe some distance checkings, but how? Any ideas?

Re: Too much packets

Posted: 29 Oct 2018, 11:32
by Lapo
Hi,
normally you need to "throttle" the key presses to avoid spamming updates.
In other words you need to define the minimum time interval between two key presses and discard everything that comes in between.

If discarding is not possible (depends on your game logic) you can simply accumulate those key presses and then send 1 single packet telling the server how many times the key has been pressed (instead of sending one packet per key press)

A good time interval could be 40-100ms, again depending on how fast is your game.

Cheers

Re: Too much packets

Posted: 29 Oct 2018, 18:34
by OhMyOhmit
Thanks, Lapo. Added a timer which solved it. Happy now :D