首页 > 解决方案 > 检查自 NodeJS 中上次间隔以来更改的属性

问题描述

在 nodeJS 中,假设我有一点setInterval

setInterval(() => {
   // ...
}, 50)

它发送给客户端实体:

setInterval(() => {
   socket.emit("entities", { ... })
}, 50)

实体看起来像这样:

{
  "0": {
    "collisionRadius": 40,
    "entityClass": "Prop",
    "health": 100,
    "maxHealth": 100,
    "model": "Tree",
    "position": {
      "x": 1970,
      "y": 1477
    },
    "uid": 0,
    "damage": 10,
    "dead": 0,
    "height": 32,
    "hits": [],
    "interpolatedYaw": 0,
    "slowed": 0,
    "stunned": 0,
    "timeDead": 0,
    "width": 32,
    "yaw": 0
  },
  "1": {
    "collisionRadius": 40,
    "entityClass": "Prop",
    "health": 100,
    "maxHealth": 100,
    "model": "Tree",
    "position": {
      "x": 1240,
      "y": 1866
    },
    "uid": 1,
    "damage": 10,
    "dead": 0,
    "height": 32,
    "hits": [],
    "interpolatedYaw": 0,
    "slowed": 0,
    "stunned": 0,
    "timeDead": 0,
    "width": 32,
    "yaw": 0
  },
  "2": {
    "collisionRadius": 40,
    "entityClass": "Prop",
    "health": 100,
    "maxHealth": 100,
    "model": "Tree",
    "position": {
      "x": 2195,
      "y": 2034
    },
    "uid": 2,
    "damage": 10,
    "dead": 0,
    "height": 32,
    "hits": [],
    "interpolatedYaw": 0,
    "slowed": 0,
    "stunned": 0,
    "timeDead": 0,
    "width": 32,
    "yaw": 0
  }
}

我如何在每个时间间隔只发送所有实体的更新属性,如果所有实体都没有改变,只发送true

接收到的实体应如下所示:

{
  "0": true,
  "1": {
    "health": 99,
    "position": {
      "x": 1245, /** position and health changed, send them **/
      "y": 1866 /** even if only the x changed, send y too **/
    }
  },
  "2": true /** all the entity stayed the same, so send `true` **/
}

标签: javascriptnode.jsobject

解决方案


推荐阅读