Node.js 中的 agent.maxSockets 属性
该agent.maxSockets属性定义了代理可以为每个源同时打开的套接字数。默认情况下,此值设置为Infinity。这也是“http”模块的一部分。
语法
agent.maxSockets: number
参数
上述函数可以接受以下参数-
number –这定义了代理可以拥有的并发套接字数。其默认值设置为无穷大。
示例
创建一个具有名称的文件-maxSockets.js并复制以下代码片段。创建文件后,使用以下命令运行此代码,如下例所示:
node maxSockets.js
最大套接字.js
//agent.maxSocketsmethod演示示例 // Importing the http & agentkeepalive module const http = require('http'); const agent = require('agentkeepalive'); const keepaliveAgent = new agent({ maxSockets: 100, maxFreeSockets: 10, timeout: 60000, //活动套接字保活60秒 freeSocketTimeout: 30000, //空闲套接字保活30秒 }); const options = { host: 'nhooo.com', port: 80, path: '/', method: 'GET', agent: keepaliveAgent, }; console.log("最大空闲插槽数: ",keepaliveAgent.maxSockets); console.log('[%s] agent status changed: %j', Date(), keepaliveAgent.getCurrentStatus());输出结果
C:\home\node>> 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":{}}