【问题标题】:Javascript Maximum call stack size exceeded errorJavascript最大调用堆栈大小超出错误
【发布时间】:2020-12-23 01:22:23
【问题描述】:

我正在使用 socket.io 从服务器发送和接收数据。我有一个“创建游戏”按钮客户端,允许我在服务器端创建一个新的 GameServer。从那里,我在客户端显示 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

【问题讨论】:

    标签: javascript node.js express socket.io


    【解决方案1】:

    您的问题发生是因为您将socket 对象存储在GameServer.sockets 数组中并且套接字对象是循环的。在您的 serv() 函数中发出该数组时,您面临的问题是它试图发送整个 socket 对象,并在其中进入递归。考虑不将整个 socket 保存在数组中,这似乎是合理的,或者不发出整个 socket 对象。如果你 console.log() socket,你会看到那些循环字段。

    【讨论】:

      猜你喜欢
      • 2011-08-31
      • 1970-01-01
      • 1970-01-01
      • 2019-05-21
      • 1970-01-01
      • 2019-07-19
      • 1970-01-01
      • 2021-09-21
      • 2012-06-26
      相关资源
      最近更新 更多