Garmaine Staff asked 2 years ago

I am making a clone of agario in javascript and i want the server to send to the client a complex javascript object that i call 'GameMap'. This object contains a hierarchy of multiple javascript objects (PlayerGroup, Player, FoodGroup, Food, and so on …)

enter image description here

I know that with socket.io i can use io.sockets.emit to send javascript objects from the server to the client, but i noticed that when sending my objects they don't keep their types.

So I tried to send my objects using JSON.stringify and JSON.parse with a replacer and a reviver functions and it works, but I need to edit both functions for each type of object i want to send.

In a nutshell, I wonder: Is there is a simple way to send my GameMap object so that all my sub-objects keep their types without using JSON?