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

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

Mô tả

Thuộc tính agent.maxFreeSockets định nghĩa số socket được để mở ở trạng thái rảnh (free state). Đây là một phần của giao diện module http.

Cú pháp

agent.maxFreeSockets : number

Tham số

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

  • number – Định nghĩa số socket có thể giữ mở ở trạng thái rảnh. Giá trị mặc định là 256.

Ví dụ

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

node maxFreeSockets.js

maxFreeSockets.js

// agent.maxFreeSockets 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.maxFreeSockets); console.log('[%s] agent status changed: %j', Date(), keepaliveAgent.getCurrentStatus());

Đầu ra

C:\home\node>> node maxFreeSockets.js Max free sockets: 10 [Fri Apr 30 2021 12:21:12 GMT+0530 (India Standard Time)] agent status changed: {"createSocketCount":0,"createSocketErrorCount":0,"closeSocketCount":0,"errorSocketCount":0,"timeoutSocketCount":0,"requestCount":0,"freeSockets":{},"sockets":{},"requests":{}}