2

If you look at websocket games like browserquest, where is the player data stored? Is it in the websocket server .js file?

Because if, for example, a new player joins the game, the game has to be rendered in its current state, with all players, items etc. Where is this data stored?

Johan
  • 35,120
  • 54
  • 178
  • 293

2 Answers2

3

Online games usually have a central server component which manages the world state. Each client communicates directly with the server. The server sends each client data about the surrounding (that's how the map looks, there is a mob A, and there is an item B) and the clients sends the server information about what it is doing (I walk to x:y; I attack mob A; I pick up item B).

In Mozilla Browserquest, this server component is programmed in NodeJS.

Regarding persisting the game state of players which are currently logged out: This should also be done on the server by storing this data in some kind of database. A possible but much worse alternative would be to store the players progress in the local Webstorage of the client (another new HTML5 feature). That would relieve the server of the responsibility, but would give players an easy opportunity to cheat, because they were able to edit their game state.

Philipp
  • 67,764
  • 9
  • 118
  • 153
  • What about storing the players as objects in the javascript serverfile? And remove then in the disconnect event. I dont mind that the player data isnt saved when they log out in this case. – Johan Sep 23 '12 at 11:59
  • To avoid any misunderstandings, Please define what you mean with "javascript serverfile" exactly. – Philipp Sep 23 '12 at 12:01
  • The .js file that i place in my nodejs folder an runs from windows cmd using `node server.js`. Sorry about not mentioning node.js earlier btw – Johan Sep 23 '12 at 12:04
  • For performance reasons it would generally be a good idea for the server to keep any data about currently logged in players in memory, and only suspend them to the database when they log out. – Philipp Sep 23 '12 at 12:05
  • Thanks, thats just what i needed to know – Johan Sep 23 '12 at 12:06
  • in this case we talk about so called "Sessions", right? In this case Node.js provides something else. Or some plugin, please look at this SO question http://stackoverflow.com/questions/3737062/managing-sessions-in-nodejs – Luke Sep 23 '12 at 14:13
  • 1
    @Luke: In the context of web developments, sessions are usually a tool to track users between page accesses. But in a game like Browserquest, the user stays on the same html page all the time. A javascript on this website keeps a steady websocket connection to the server. So classic web session handling is not required or only plays a minor role in this case. – Philipp Sep 23 '12 at 16:30
  • yeah, but even if you stay on the same page, and then it hangs, something goes wrong (happens even to gmail) you need to refresh, without using sessions or cookies you are not able to connect the lost user to its previous state – Luke Sep 23 '12 at 18:29
  • @Philipp - How would we store the data in memory? Is there any equivalent of an application scope? (data that can be read/written to and is available to all users/sessions) – Luke Dec 16 '13 at 23:20
2

Thats up to you where you save it on the server side. I was looking yesterday some nice screencast about meteor, which uses node.js, mongoDB. It is a framework that allows to do exactly what you are talking about.

Luke
  • 8,235
  • 3
  • 22
  • 36
  • But would it be a bad idea to store each player as an object in the javascript serverfile? – Johan Sep 23 '12 at 11:53