Socket.IO bare-bones no-method objects or fully-equipped class-based objects for client updates?

℡╲_俬逩灬. 提交于 2019-12-25 06:31:08

问题


Say I have an array of players in my Node app, each player being an object. Every tick, I send my array to my clients, with Socket.IO. I'm not sure, out of the two following methods, would be the most efficient way of doing this:

  1. For each player, I add a basic object to my array, such as {id:1,x:10,y:20,color:"#000000"}. Then I can send the whole array, raw, to my clients. Functions that I need are non-object specific and handle the parsed object. This options is used in this Agar.io clone.
  2. For each player, I add an object based on a Player class, like {id:1,x:10,y:20,color:"#000000",update:function(){//code},someOtherFunction:function(){//code}} and then create a pseudo array of pseudo objects (like the one in option 1) to send to my players each tick.

If there are any better alternatives, please outline them.


回答1:


Data stored in your app should be optimized for best coding style. That means it should be readable, maintainable, extensible, robust, etc...

Data sent via a transport should be optimized for most efficient transport.

One should not really influence or drive the other. If the two separate priorities lead to different formats, then you simply convert from one to the other before sending data or when receiving data.


Given all that, I would think that you would want to use object oriented principles within your app so that a player could be an actual object with methods.

Then, when you want to send data across the wire, you just pull out whatever data you want to send, format it into something that is efficient for transport and send it. The receiver will then parse the data and insert it into an object.



来源:https://stackoverflow.com/questions/36932999/socket-io-bare-bones-no-method-objects-or-fully-equipped-class-based-objects-for

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!