Tuesday, March 26, 2019

networking - Tips for communication between JS browser game and node.js server?




I am tinkering around with some simple Canvas based cave flyer game and I would like to make it multiplayer eventually. The plan is to use Node.js on the server side.


The data sent over would consists of position of each player, direction, velocity and such. The player movements are simple force physics, so I should be able to extrapolate movements before next update from server.


Any tips or best practices on the communications side? I guess web sockets are the way to go. Should I send information in every pass of the game loop or with specified intervals? Also, I don't mind if it doesn't work with older browsers.



Answer



I recently made a asteroids/geometry wars crossover with Node.js / JavaScript:
http://github.com/BonsaiDen/NodeGame-Shooter


It has a fat Server which processes the game and thin clients which are basically just views. The client does some interpolation and things to make it look smooth.


You may want to look at these two files, which contain the underlying networking logic, as well as the actor and client models:
http://github.com/BonsaiDen/NodeGame-Shooter/blob/master/client/nodegame.client.js
http://github.com/BonsaiDen/NodeGame-Shooter/blob/master/server/nodegame.js



The whole game is based on Actors which handle their "update events"(the stuff that gets send to the clients) on their own, for the most part. It's also possible to hide actors from specific client to implement, for example, invisibility.


One can also record games and just feed the messages into the client to play them back.


As far as the tech goes:
WebSockets are the way to go here. I also did a custom binary encoding for JS that, while giving up on things like more than 2 decimal places on floats, is about 50% smaller than JSON(and is 2x as fast under V8 than native JSON encoding)


No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...