我正在尝试创建一个nodejs服务器,我正在运行一个简单的x/y世界,客户端可以从中推/拉数据。如果我只在box2d或类似的东西上在客户端做这样的世界模拟,我会使用setTimeout函数来调用step函数。这在我在nodejs中尝试时不起作用。服务器崩溃,出现错误“RangeError:超出最大调用堆栈大小”。nodejs服务器端模拟
这是我的server.js。 world参数是路由器可以操作的世界对象的实例。
var http = require("http");
function start(world, router, handlers) {
function onRequest(request, response) {
router(world, handlers, request, response);
}
http.createServer(onRequest).listen(8888);
console.log("ServerStarted. Listens to 8888.");
step_world(world,0);
}
function step_world(world,step) {
world.update();
step++;
console.log("updating world: " + step);
step_world(world,step);
//setTimeout(step_world(world,step),1000/30);
}
exports.start = start;
所以:我如何使用nodejs运行模拟?
setInterval会但我不相信setTimeout会导致多次调用。 –