Too much packets

Post here your questions about the Unity / .Net / Mono / Windows 8 / Windows Phone 8 API for SFS2X

Moderators: Lapo, Bax

OhMyOhmit
Posts: 18
Joined: 24 Apr 2018, 12:25

Too much packets

Postby OhMyOhmit » 29 Oct 2018, 09:32

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?
User avatar
Lapo
Site Admin
Posts: 23008
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Too much packets

Postby Lapo » 29 Oct 2018, 11:32

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
Lapo
--
gotoAndPlay()
...addicted to flash games
OhMyOhmit
Posts: 18
Joined: 24 Apr 2018, 12:25

Re: Too much packets

Postby OhMyOhmit » 29 Oct 2018, 18:34

Thanks, Lapo. Added a timer which solved it. Happy now :D

Return to “SFS2X C# API”

Who is online

Users browsing this forum: No registered users and 22 guests