【发布时间】:2014-03-10 20:22:05
【问题描述】:
我需要在使用node.js 运行的游戏世界中广播位置更新。我目前的想法是使用BinaryJS。不过,我不确定使用它的正确方法是什么。我想到了 3 个选项:
我目前的表现如何,使用socket.io:我以编程方式存储在给定时间内发生的位置更新缓冲区,然后一次性发送:
setTimeout(function(){
//called a fixed amount of times per second (40)
//this variable will hold the accumulated position changes on the last 25ms
//var accumulated_position_changes = [id0,x0,y0,z0, id1,x1,y1,z1...];
client.send(accumulated_position_changes);
},25);
但如果binary.js 自己进行分块(是吗?),那么我是否应该在每次发生位置更改时不加选择地发送位置更改?
MyClass.prototype.set_position = function(x,y,z){
// this is called thousands times a second
this.x = x, this.y = y, this.z = z;
client.send([this.id, x, y, z]);
};
或者我应该以某种方式创建一个派生 node.js 流的对象并使用它?也许是stream.write?还是其他方式?处理这个问题的正确方法是什么?
【问题讨论】:
标签: javascript node.js networking websocket socket.io