Share your repls and programming experiences

← Back to all posts
socket io multiplayer movement
h
rafrafraf (305)

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 :)

Commentshotnewtop
realTronsi (775)

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

rafrafraf (305)

@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

realTronsi (775)

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

rafrafraf (305)

@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

realTronsi (775)

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

realTronsi (775)

@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.

rafrafraf (305)

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

realTronsi (775)

@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