คุณสมบัติ agent.maxFreeSockets กำหนดจำนวนซ็อกเก็ตที่เปิดทิ้งไว้ในขณะที่อยู่ในสถานะว่าง นี่เป็นส่วนหนึ่งของอินเทอร์เฟซโมดูล 'http'
ไวยากรณ์
agent.maxFreeSockets : number
พารามิเตอร์
ฟังก์ชันข้างต้นสามารถรับพารามิเตอร์ต่อไปนี้ได้ -
-
หมายเลข – กำหนดจำนวนซ็อกเก็ตที่สามารถเปิดได้ในสถานะว่าง ค่าเริ่มต้นคือ 256
ตัวอย่าง
สร้างไฟล์ที่มีชื่อ - maxFreeSockets.js และคัดลอกข้อมูลโค้ดด้านล่าง หลังจากสร้างไฟล์แล้ว ให้ใช้คำสั่งต่อไปนี้เพื่อเรียกใช้โค้ดนี้ดังแสดงในตัวอย่างด้านล่าง −
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()); ผลลัพธ์
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":{}}