20151026-20151030: Node.js
Last week I started reading about node.js while searching for multiplayer-solutions:
http://gamedev.stackexchange.com/a/60440/73841
I wanted to try faster alternatives to MySQL for real-time updates about every objects position. After setting up a server and writing a smaller server-side-version JavaScript, I´ve decided to import needed code from the project through an http-call. In that way I didn´t need to copy the whole project and keep changes synchronized across both versions.
So this is my setup:
“TheNest.Server” is defined in the server and listens for incoming connections. When TheNest.Server starts, it will load objects from the MySQL-database and import world-functions and brain-prototypes. Then it will run a game-loop and send back data that is being requested. (like objects positions)
“TheNest.Data.SyncClient” is defined in the browser and will try to establish and keep a connection to the server. When a connection exists, the browser will run a sync-function at regular intervals to update objects positions. If any changes are made from a browser, (like setting a destination for an object) the changes will be sent to the server.
I think this is working fast now (20 sync/second with only about 1% notice on CPU), especially after making some filters (only update objects in view and selected properties like “x,y,z”).
Maybe I will go back to using only MySQL later anyway since I´m not sure if I want to run a server. In the MySQL-case, I would probably not use 20 sync/second but instead use some status-pushing and guessing between updates.
When the server is down, the game-loop in browser will call the world-brain-function instead of synchronizing.
20151031-20151101: MySQL
I went back to synchronizing with MySQL since I want the project to be up and running even if my computer is off. This also means that I don´t need to pay as much attention to security as if I were to expose another server to the Internet, but with a drawback in performance that I will try to work around.
The first thing I improved was the updating. I made a batch-update query by using aliases on the same table like in this answer on StackOverflow (instead of sending a query through an http-request for each object to update): http://stackoverflow.com/a/3439/4181348
Later I will need to add things to keep multiple browsers/views working together instead of overwriting each other’s changes. For now, a save in intervals will do fine.