首頁 > web前端 > js教程 > 主體

Node.js 中的 agent.maxSockets 屬性

王林
發布: 2023-08-24 12:29:09
轉載
1288 人瀏覽過

Node.js 中的 agent.maxSockets 属性

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

來源:tutorialspoint.com
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!