Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> JavaScript

คุณสมบัติ agent.maxSockets ใน Node.js

คุณสมบัติ agent.maxSockets กำหนดจำนวน socket สูงสุดที่ agent สามารถเปิดพร้อมกันได้ต่อ origin (host) ค่าเริ่มต้นคือ Infinity เป็นส่วนหนึ่งของโมดูล http

ไวยากรณ์

agent.maxSockets: number

พารามิเตอร์

  • number – จำนวน concurrent sockets ต่อ host ค่าเริ่มต้น: Infinity

ตัวอย่าง: ใช้กับ agentkeepalive

const http = require('http');
const agent = require('agentkeepalive');

const keepaliveAgent = new agent({
  maxSockets: 100,
  maxFreeSockets: 10,
  timeout: 60000,
  freeSocketTimeout: 30000,
});

const options = {
  host: 'tutorialspoint.com',
  port: 80,
  path: '/',
  method: 'GET',
  agent: keepaliveAgent,
};

console.log("Max sockets: ", keepaliveAgent.maxSockets);
console.log('[%s] agent status changed: %j', Date(), keepaliveAgent.getCurrentStatus());

ผลลัพธ์

Max sockets: 100
[Fri Apr 30 2021 12:28:24 GMT+0530] agent status changed: {"createSocketCount":0,...}