简体   繁体   中英

Client-server communication, websockets latency

Short question:

Can latency, when a client is sending packets to the server suddenly change a lot?

Long question(the problem I've got):

I'm making a html game using websockets and currently working on client side input prediction, I'm using the same code for client and for server to check for collisions, calculate new player positions, 30times/second. This is how I update player movement on both server and client side:

if(controls.pressingRight === true)
{
    if(controls.dx < settings.MAX_X_SPEED)
    {
        controls.dx += settings.DELTA_X_SPEED;
    }
    else
    {
        controls.dx = settings.MAX_X_SPEED;
    }
}
if(*noCollisions*)
{
    game.me.x += controls.dx;
}

This way the player movement is kind of smooth. When a user press and holds down "D" keyboard button conrols.pressingRight value is changed to true, and it is true until player releases the "D" button. The reason I wrote my code is because I'm not sure if the problem is in my code or it has something to do with the change of latency. For example I click D button and quickly release it and my player on my client canvas moves like ~7pixels BUT on my server side player moves like 20pixels or more to the side(though sometimes both client and server side moves the same amount of pixels). I'm guessing that the problem is because when I press button down to move the player the latency might be low, let's say 20, but when I release the ping might be like 200 which means the packet was delivered later than it should've and because of that server still had my value of "pressingRight" as true. Am I missing on something?

Can latency, when a client is sending packets to the server suddenly change a lot?

Yes, absolutely. Connectivity changes occur all the time. This is especially true in mobile devices with antennas moving around quite a bit, causing momentarily loss in connectivity.

I'm making a html game using websockets and currently working on client side input prediction, I'm using the same code for client and for server to check for collisions, calculate new player positions, 30times/second.

...

When a user press and holds down "D" keyboard button conrols.pressingRight value is changed to true, and it is true until player releases the "D" button.

This is definitely one way, but if you go this route the server should be your source of truth as to the state of things. The client can move to where it think it moved, and then the server can update positions, bouncing the client back to the standard server-known version of events. In the optimal situation, the client won't move by much. However, lag happens, and sometimes that client is going to snap back to a location much further away.

There is no perfect solution to this problem when the game requires such interactivity. However, there are many ways to address it. See also: http://gafferongames.com/networking-for-game-programmers/what-every-programmer-needs-to-know-about-game-networking/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM