คุณสามารถใช้เมธอด Agent() ใหม่เพื่อสร้างอินสแตนซ์ของเอเจนต์ใน Node เมธอด http.request() ใช้ globalAgent จากโมดูล 'http' เพื่อสร้างอินสแตนซ์ http.Agent ที่กำหนดเอง
ไวยากรณ์
new Agent({options}) พารามิเตอร์
ฟังก์ชันข้างต้นสามารถยอมรับ พารามิเตอร์ . ต่อไปนี้ −
-
ตัวเลือก – ตัวเลือกเหล่านี้จะประกอบด้วยตัวเลือกที่กำหนดค่าได้ซึ่งสามารถตั้งค่าบนตัวแทนได้ในขณะสร้าง ด้านล่างนี้คือฟิลด์/ตัวเลือกที่ตัวแทนสามารถมีได้ -
-
รักษาชีวิต – วิธีนี้ช่วยให้ซ็อกเก็ตอยู่รอบ ๆ ไม่ว่าจะมีคำขอที่ค้างอยู่หรือไม่ แต่เก็บไว้สำหรับคำขอในอนาคตโดยไม่ต้องสร้างการเชื่อมต่อ TCP ใหม่จริง ๆ หนึ่งสามารถใช้การเชื่อมต่อ 'ปิด' เพื่อปิดการเชื่อมต่อนี้ ค่าเริ่มต้น:เท็จ
-
KeepAliveMsecs – ในการใช้ตัวเลือก keepAlive เป็นจริง ตัวเลือกนี้จะกำหนดความล่าช้าเริ่มต้นสำหรับแพ็กเก็ต TCP keep-Alive ค่าเริ่มต้นคือ 1,000
-
maxSockets – ตัวเลือกนี้กำหนดจำนวนสูงสุดของซ็อกเก็ตที่อนุญาตต่อโฮสต์ โดยค่าเริ่มต้น ค่านี้คืออนันต์
-
maxTotalSockets – จำนวนซ็อกเก็ตทั้งหมดที่อนุญาตสำหรับโฮสต์ทั้งหมด คำขอแต่ละรายการใช้ซ็อกเก็ตใหม่จนกว่าจะถึงขีดจำกัด ค่าเริ่มต้นคืออนันต์
-
maxFreeSockets - นี่คือจำนวนสูงสุดของซ็อกเก็ตว่างที่สามารถเปิดทิ้งไว้ในสถานะว่างได้ ค่าเริ่มต้นคือ:256
-
กำหนดการ – นี่คือกลยุทธ์การจัดตารางเวลาที่สามารถใช้ได้ในขณะที่เลือกซ็อกเก็ตว่างตัวต่อไปที่จะใช้ การจัดกำหนดการอาจเป็น 'fifo' หรือ 'lifo' ก็ได้
-
หมดเวลา – แสดงถึงการหมดเวลาของซ็อกเก็ตในหน่วยมิลลิวินาที
-
ตัวอย่าง
สร้างไฟล์ที่มีชื่อ – agent.js และคัดลอกข้อมูลโค้ดด้านล่าง หลังจากสร้างไฟล์แล้ว ให้ใช้คำสั่งต่อไปนี้เพื่อเรียกใช้โค้ดนี้ดังแสดงในตัวอย่างด้านล่าง −
node agent.js
agent.js
// Node.js program to demonstrate the creation of new Agent
// Importing the http module
const http = require('http');
// Creating a new agent
var agent = new http.Agent({});
// Defining options for agent
const aliveAgent = new http.Agent({
keepAlive: true, maxSockets: 5,
});
// Creating connection with alive agent
var aliveConnection = aliveAgent.createConnection;
// Creating new connection
var connection = agent.createConnection;
// Printing the connection
console.log('Succesfully created connection with agent: ',
connection.toString);
console.log('Succesfully created connection with alive agent: ',
aliveConnection.toString); ผลลัพธ์
C:\home\node>> node agent.js
Succesfully created connection with agent: function toString() { [native code] }
Succesfully created connection with alive agent: function toString() { [native code] } ตัวอย่าง
โมดูล 'agentkeepalive' ให้ความยืดหยุ่นที่ดีขึ้นในขณะที่พยายามสร้างซ็อกเก็ตหรือตัวแทน เราจะใช้โมดูลนี้ในตัวอย่างด้านล่างเพื่อความเข้าใจที่ดีขึ้น
การติดตั้ง
npm install agentkeepalive --save
รหัสโปรแกรม
// Node.js program to demonstrate the creation of new Agent
// Importing the http module
const http = require('http');
// Importing the agentkeepalive module
const Agent = require('agentkeepalive');
// Creating a new agent
const keepAliveAgent = new Agent({});
// Implementing some options
const options = {
host: 'tutorialspoint.com',
port: 80,
path: '/',
method: 'GET',
agent: keepAliveAgent,
};
// Requesting details via http server module
const req = http.request(options, (res) => {
// Printing statuscode, headers and other details
// received from the request
console.log("StatusCode: ", res.statusCode);
console.log("Headers: ", res.headers);
});
// Printing the agent options
console.log("Agent Options: ", req.agent.options);
req.end(); ผลลัพธ์
C:\home\node>> node agent.js
Agent Options: { socketActiveTTL: 0,
timeout: 30000,
freeSocketTimeout: 15000,
keepAlive: true,
path: null }
StatusCode: 403
Headers: { date: 'Sun, 25 Apr 2021 08:21:14 GMT',
server: 'Apache',
'x-frame-options': 'SAMEORIGIN',
'last-modified': 'Thu, 16 Oct 2014 13:20:58 GMT',
etag: '"1321-5058a1e728280"',
'accept-ranges': 'bytes',
'content-length': '4897',
'x-xss-protection': '1; mode=block',
vary: 'User-Agent',
'keep-alive': 'timeout=5, max=100',
connection: 'Keep-Alive',
'content-type': 'text/html; charset=UTF-8' }