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中文網其他相關文章!