agent.maxSockets 属性定义代理可以为每个源同时打开的套接字数量。默认情况下,该值设置为无穷大。这也是“http”模块的一部分。
agent.maxSockets: number
上述函数可以接受以下参数 -
number – 这定义了代理可以拥有的并发套接字数。其默认值设置为 Infinity。
创建一个名为 maxSockets.js 的文件并复制以下代码片段。创建文件后,使用以下命令运行此代码,如下例所示:
node maxSockets.js
maxSockets.js
// agent.maxSockets method Demo example // Importing the http & agentkeepalive module const http = require('http'); const agent = require('agentkeepalive'); const keepaliveAgent = new agent({ maxSockets: 100, maxFreeSockets: 10, timeout: 60000, // active socket keepalive for 60 seconds freeSocketTimeout: 30000, // free socket keepalive for 30 seconds }); const options = { host: 'tutorialspoint.com', port: 80, path: '/', method: 'GET', agent: keepaliveAgent, }; console.log("Max free sockets: ",keepaliveAgent.maxSockets); console.log('[%s] agent status changed: %j', Date(), keepaliveAgent.getCurrentStatus());
C:\homeode>> node maxSockets.js Max sockets: 100 [Fri Apr 30 2021 12:28:24 GMT+0530 (India Standard Time)] agent status changed: {"createSocketCount":0,"createSocketErrorCount":0,"closeSocketCount":0,"errorS ocketCount":0,"timeoutSocketCount":0,"requestCount":0,"freeSockets":{},"socket s":{},"requests":{}}
以上是Node.js 中的 agent.maxSockets 属性的详细内容。更多信息请关注PHP中文网其他相关文章!