javascript - 检查自 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` **/
}
解决方案
推荐阅读
- javascript - 反应水平滚动菜单从左箭头到 item1 占用额外空间
- python - 仅返回列表的第一个输入且不在输入中继续的函数超出范围
- angular - 切换回来后,角度动画在 mat-tabs 中不起作用
- istio - 使用 istio 入口网关托管多个 Web 应用程序
- mysql - MariaDB DATE 到文本,哪种方式?
- java - Exoplayer 如何为直播手动设置持续时间/位置?
- mule - java.lang.NoClassDefFoundError:com/google/common/util/concurrent/internal/InternalFutureFailureAccess
- directx - 有没有办法在 Windows 7 中使用 DirectX 11?
- node.js - 在VSCode上调试nodejs时找不到模块app.js
- python - 我无法将我的 py 文件打包成 exe 文件