Thuộc tính agent.maxFreeSockets xác định số lượng ổ cắm được để mở khi ở trạng thái trống. Đây là một phần của giao diện mô-đun 'http'.
Cú pháp
agent.maxFreeSockets : number
Tham số
Hàm trên có thể chấp nhận các tham số sau -
-
số - Điều này xác định số lượng ổ cắm có thể được mở ở trạng thái trống. Giá trị mặc định của nó được đặt thành 256.
Ví dụ
Tạo một tệp có tên - maxFreeSockets.js và sao chép đoạn mã bên dưới. Sau khi tạo tệp, sử dụng lệnh sau để chạy mã này như được hiển thị trong ví dụ bên dưới -
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,"errorS
ocketCount":0,"timeoutSocketCount":0,"requestCount":0,"freeSockets":{},"socket
s":{},"requests":{}}