Socket.IO为客户端更新提供了无方法对象或完全配备的基于类的对象? [英] Socket.IO bare-bones no-method objects or fully-equipped class-based objects for client updates?

查看:125
本文介绍了Socket.IO为客户端更新提供了无方法对象或完全配备的基于类的对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我的Node应用程序中有一个玩家数组,每个玩家都是一个对象。每个滴答,我用Socket.IO将我的阵列发送给我的客户。我不确定,在以下两种方法中,最有效的方法是:

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. 每个 player ,我在我的数组中添加了一个基本对象,例如 {id:1,x:10,y:20,color:#000000} 。然后我可以将整个阵列原始发送给我的客户。我需要的函数是非对象特定的并处理解析的对象。此选项用于此 Agar.io克隆

  2. 对于每个播放器,我添加一个基于播放器类的对象,如 {id:1,x:10,y:20,颜色:#000000,更新:function(){// code},someOtherFunction:function(){// code}} 和然后创建一个伪对象的伪数组(如选项 1 中的那个),发送给我的玩家每个刻度。

  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.

推荐答案

应优化应用中存储的数据以获得最佳效果编码风格。这意味着它应该是可读的,可维护的,可扩展的,健壮的等等......

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.

这篇关于Socket.IO为客户端更新提供了无方法对象或完全配备的基于类的对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆