gpt4 book ai didi

Javascript 超出最大调用堆栈大小错误

转载 作者:行者123 更新时间:2023-12-02 22:24:34 25 4
gpt4 key购买 nike

我正在使用 socket.io 从服务器发送和接收数据。我有一个“创建游戏”按钮客户端,允许我在服务器端创建一个新的游戏服务器。从那里,我在客户端显示 game.show() 内的信息。单击“创建游戏”后,我的所有 console.log 消息(服务器端和客户端)都将被发送,但随后服务器停止并给出此错误。

buffer.js:407
return b instanceof Buffer;
^
RangeError: Maximum call stack size exceeded
at Function.[Symbol.hasInstance] (<anonymous>)
at Function.isBuffer (buffer.js:407:12)
at hasBinary (/rbd/pnpm-volume/09796244-83a8-4b11-b56a-df421860dd93/node_modules/.registry.npmjs.org/has-binary2/1.0.3/node_modules/has-binary2/index.js:44:66)
at hasBinary (/rbd/pnpm-volume/09796244-83a8-4b11-b56a-df421860dd93/node_modules/.registry.npmjs.org/has-binary2/1.0.3/node_modules/has-binary2/index.js:58:59)
at hasBinary (/rbd/pnpm-volume/09796244-83a8-4b11-b56a-df421860dd93/node_modules/.registry.npmjs.org/has-binary2/1.0.3/node_modules/has-binary2/index.js:58:59)
at hasBinary (/rbd/pnpm-volume/09796244-83a8-4b11-b56a-df421860dd93/node_modules/.registry.npmjs.org/has-binary2/1.0.3/node_modules/has-binary2/index.js:58:59)
at hasBinary (/rbd/pnpm-volume/09796244-83a8-4b11-b56a-df421860dd93/node_modules/.registry.npmjs.org/has-binary2/1.0.3/node_modules/has-binary2/index.js:58:59)
at hasBinary (/rbd/pnpm-volume/09796244-83a8-4b11-b56a-df421860dd93/node_modules/.registry.npmjs.org/has-binary2/1.0.3/node_modules/has-binary2/index.js:58:59)
at hasBinary (/rbd/pnpm-volume/09796244-83a8-4b11-b56a-df421860dd93/node_modules/.registry.npmjs.org/has-binary2/1.0.3/node_modules/has-binary2/index.js:58:59)
at hasBinary (/rbd/pnpm-volume/09796244-83a8-4b11-b56a-df421860dd93/node_modules/.registry.npmjs.org/has-binary2/1.0.3/node_modules/has-binary2/index.js:58:59)

这是当我单击“创建服务器”按钮时运行的内容

    if (this.add.clicked()) {
var sid = Math.random().toString(36).substr(2, 5);
console.log('1');
emitToServer('create', sid);
console.log('2');
emitToServer('addtoserver', sid);
console.log('3');
console.log('4');
state = "GAME";
console.log('5');
}

这是我在服务器端获取“create”和“addtoserver”消息的地方

    socket.on('create', function(id) {
console.log('3');
console.log("Creating server " + id);
servers.set(id, new GameServer(id));
console.log(servers);
console.log('d');
});

socket.on('addtoserver', function(sid) {
try {
console.log('1');
console.log('Adding socket to ' + servers.get(sid));
servers.get(sid).addClient(socket);
console.log('pork');
}
catch (err) {
console.log('error');
}
});

完整代码:Github

最佳答案

出现问题的原因是您将 socket 对象存储在 GameServer.sockets 数组中,并且套接字对象是循环的。当在 serv() 函数中发出该数组时,您面临着它尝试发送整个 socket 对象并在其中输入递归的问题。考虑要么不将整个 socket 保存在数组中(这似乎是合理的),要么不发出整个 socket 对象。如果您 console.log() socket,您将看到那些圆形字段。

关于Javascript 超出最大调用堆栈大小错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59108781/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com