简体   繁体   English

TypeError:将圆形结构转换为JSON

[英]TypeError: Converting circular structure to JSON

In my application i am creating an object to keep track of all sockets; 在我的应用程序中,我正在创建一个对象来跟踪所有套接字。

I declare it like: 我这样声明:

var sockets = {};

Then when a new socket is created i add the socket to my sockets object; 然后,当创建一个新的套接字时,我将该套接字添加到我的sockets对象中;

sockets[socket.name] = socket;

This is required for the logic of my application. 这是我的应用程序逻辑所必需的。 And it works perfectly. 而且效果很好。

The problem i am having is when i want to store the sockets object to a file (for backup purposes) due to my server sometimes restarting. 我遇到的问题是由于服务器有时重新启动,我想将sockets对象存储到文件中(出于备份目的)。 I dont want active sockets to get lost if a restart should happend. 如果重新启动,我不希望活动套接字丢失。

I've tried to save it to a file like this: 我试图将其保存到这样的文件中:

    var outputFilenameee = 'sockets.json';

    fs.writeFile(outputFilenameee, JSON.stringify(sockets), function(err) {
        if(err) {
          console.log(err);
        } else {
          console.log("sockets saved in " + outputFilenameee);
        }           

    });

But i am getting this error: 但我收到此错误:

TypeError: Converting circular structure to JSON TypeError:将圆形结构转换为JSON

Below is a console.log of sockets : 以下是sockets的console.log:

{ '217.208.204.18:63695': 
   { domain: null,
     _events: { data: [Function], end: [Function] },
     _maxListeners: 10,
     _handle: 
      { writeQueueSize: 0,
        owner: [Circular],
        onread: [Function: onread] },
     _pendingWriteReqs: 0,
     _flags: 0,
     _connectQueueSize: 0,
     destroyed: false,
     errorEmitted: false,
     bytesRead: 125,
     _bytesDispatched: 0,
     allowHalfOpen: false,
     writable: true,
     readable: true,
     server: 
      { domain: null,
        _events: [Object],
        _maxListeners: 10,
        _connections: 1,
        connections: [Getter/Setter],
        allowHalfOpen: false,
        _handle: [Object],
        _connectionKey: '4:0.0.0.0:8080' },
     _peername: { address: '217.208.204.18', family: 'IPv4', port: 63695 },
     name: '217.208.204.18:63695' } }

How can i store this in a file? 如何将其存储在文件中?

I dont want active sockets to get lost if a restart should happend 我不希望活动套接字在重新启动时丢失

Sorry, but this is unavoidable given the process model of operating systems. 抱歉,但是鉴于操作系统的过程模型,这是不可避免的。 Your process's resources including network sockets, open files, memory, etc, are freed and reclaimed by the OS when the process exits. 当进程退出时,操作系统将释放并回收您进程的资源,包括网络套接字,打开的文件,内存等。

In order to achieve something like this you would need a separate load balancer or cluster manager to keep the raw sockets alive while one cluster node/worker restarts. 为了实现这样的目标,您需要一个单独的负载平衡器或群集管理器,以在一个群集节点/工作程序重新启动时保持原始套接字保持活动状态。 You may want to investigate the node.js cluster module, but given you tried to do this by writing sockets to disk, there are some gaps in your understanding of the OS at this level that you may want to study up a bit more before venturing outside of regular application logic. 您可能想研究一下node.js集群模块,但是鉴于您尝试通过将套接字写入磁盘来完成此操作,因此在此级别上对操作系统的理解存在一些空白,您可能需要在学习之前进一步研究一下在常规应用程序逻辑之外。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM