Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> JavaScript

Thuộc tính agent.maxSockets trong Node.js

Mô tả

Thuộc tính agent.maxSockets định nghĩa số socket có thể mở đồng thời bởi agent cho mỗi origin. Mặc định giá trị này là Infinity. Đây cũng là một phần của module http.

Cú pháp

agent.maxSockets: number

Tham số

Hàm trên có thể chấp nhận các Tham số sau:

  • number – Định nghĩa số socket đồng thời một agent có thể có. Giá trị mặc định là Infinity.

Ví dụ

Tạo file tên maxSockets.js và sao chép đoạn mã dưới. Sau khi tạo file, dùng lệnh sau để chạy:

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());

Đầu ra

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,"errorSocketCount":0,"timeoutSocketCount":0,"requestCount":0,"freeSockets":{},"sockets":{},"requests":{}}