Skip to content
Sign upLog in

socket io multiplayer movement

Profile icon
rafrafraf

playing around with socket io and made this so far.

its definitely not done and is quite laggy but i think its a cool start

try opening it in 2 tabs and see the movement update on both :)

You are viewing a single comment. View All
Profile icon
realTronsi

Wow, it is quite laggy, how are you sending the socket.io packets?

Profile icon
rafrafraf

@realTronsi
yeah its really laggy, im trying to think of how i can make it more efficient. at the moment everytime anyone moves the server broadcasts every user and their position and each client goes through that broadcast and updates each element position

Profile icon
realTronsi

@rafrafraf
Hmm, are you handling the physics in the server side?

Profile icon
rafrafraf

@realTronsi
yep, i have all the players as instances of the class Player which includes their name and x y pos.
i know how im doing it is stupid i just cant think of how else to do it, got any ideas? thanks

Profile icon
realTronsi

@rafrafraf
Hmm I don't think so, I don't see a script for player movement on the server side.

Profile icon
rafrafraf

@realTronsi
thats cool

Profile icon
realTronsi

@rafrafraf
???

Afaik you're handling movement on the client side, which is a big no-no for these kind of applications. Instead, give each player an array for their key presses on the server side, and on a set interval, move the player. If the player releases the key, send over another message.

That way all players will move at the same speed. Since you're handling movement on the client side right now, players with powerful computers will move faster than players with slower computers.

Profile icon
rafrafraf

@realTronsi
im not really sure still how id handle movement server side...

Profile icon
realTronsi

@rafrafraf

On the client side do something like this:

When key press: send message to server when key release: send message to server

And in the server:

When client joins: {"id": some id, "keyPresses": []} When client presses a key: client.keyPresses.append(key pressed) When client releases a key: Remove key from client.keyPresses Main loop: if(client pressed w is true) move up etc.

I didn't write any exact code since I don't have time to do that and it's up to you to figure that out, but that's the general outline

Profile icon
rafrafraf

@realTronsi
thanks