I am writing a project using node.js + socket.io for an online game limited in time (1 hour) and number of users (20-30).
The task is to store general data (data object) of the game state in the memory of the node, for the duration of the game, which can be accessed globally (read / write) for each connected user of the game participant. How to implement it in node.js?
- oneIt is better not to store any data in the memory, it is likely to shoot up. Do not be afraid to organize such things in the database. - etki
- oneredis will be very useful for your task - Vladimir Gamalyan
- Yes, I also consider these options for use - store everything in the database (mysql, mongodb, redis). But in this case, the data storage mechanism in the node is of interest according to a principle such as socket.io, for example, stores a list of room users available to each user of this room in the global area - D.Leem
- oneIf you want to store it in memory, you can make a global JSON in it to create the game id, and already in the id object to store data - pnp2000
- @ vnn198 Yes, my problem was that this json object was defined by var inside io.sockets.on ('connection', function (socket) {...}); and therefore it was not visible globally. Brought out and the object became visible globally. Thank. - D.Leem
|
1 answer
In order to keep in memory nodes data available for each user, it is enough to create a global variable (object) at the beginning of the script, and save the data for the current game there. This variable will be visible and editable for each logged in user.
PS The problem was originally that the object was defined via a var inside io.sockets.on ('connection', function (socket) {...}); and therefore it was not visible globally
- and how did you achieve the appearance of a global variable in different processes (workers)? if they are not there yet, then sooner or later you will have a cluster and these problems - Mi Ke Bu
|