Reputation: 55
Hello I'm learning javascript by making game with socket.io and node.js. I would ask you about how it should work. Now all positions of players, player's bullets and other stuff are stored on server and server emit this to client. Client when get data draw everything on screen. Is that everything okay ? When I deploy server.js file on free node.js server I get lags but when I run it on my PC I didnt get it. Is this server fault ?
I can paste link to repository if it will help and thanks for help.
Upvotes: 0
Views: 1502
Reputation:
I look into your code in repository and it's kinda naive. My main suggestions:
1) Don't send updated data to often. You are trying send client data every 33ms, but it's kinda heavy even for native games. Send data every second, for sync world state, and extrapolate it on client (just visually part, not internal logic... e.g. emulate bullets fly and collisions, but not destroying objects, it's servers jobs).
2) Send only new data. Try to send only diffs. It's very tricky and hard, but worth of work. You may combine it with p. 1 and, for example, every 5 seconds send internal state and every 0.5 sec send diff with previously state.
3) Send only events. If bullet fly 50 frames straightforward than their is no need to send 50 positions. Just send "Bullet #uniqid start flying from XY with speed +1, 0" and than send "Bullet #uniqid blowup at X, Y and destroyed objects [list of id's]"
Sorry for my bad English, I hope you understand my suggestions.
Upvotes: 1
Reputation: 5683
Make sure that the server you deploy to permits websocket connections. socket.io will switch to long polling via http if it's not able to use websocket. This may cause the the lags you experience.
You may also check this in the browser web console by inspecting the network traffic and see if packets are sent over the websocket or http.
Upvotes: 2