ভূমিকা
agent.createConnection() মেথড http মডিউলের একটি ইন্টারফেস। এটি HTTP রিকোয়েস্টের জন্য ব্যবহারযোগ্য একটি সকেট/স্ট্রিম তৈরি করে। কাস্টম এজেন্ট ব্যবহার করে এই মেথডকে ওভাররাইড করা যায় আরও ফ্লেক্সিবিলিটির জন্য। সকেট/স্ট্রিম দুটি উপায়ে রিটার্ন করা যায়—সরাসরি ফাংশন থেকে রিটার্ন করে অথবা কলব্যাকের মাধ্যমে পাস করে।
সিনট্যাক্স
agent.createConnection(options, [callback])
প্যারামিটার
options — স্ট্রিম তৈরির জন্য কানেকশন ডিটেইলস থাকবে।
callback — এজেন্ট থেকে তৈরি সকেট কানেকশন রিসিভ করবে।
উদাহরণ ১
connection.js নামে ফাইল তৈরি করে নিচের কোড কপি করুন। তারপর node connection.js কমান্ড দিয়ে রান করুন।
// Node.js program to demonstrate the creation of socket // using agent.createConnection() method // Importing the http module const http = require('http'); // Creating a new agent var agent = new http.Agent({}); // Creating connection with the above agent var conn = agent.createConnection; console.log('Connection is succesfully created !'); // Printing connection details console.log('Connection: ', conn);আউটপুট
C:\home\node>> node connection.js Connection is succesfully created ! Connection: function connect(...args) { var normalized = normalizeArgs(args); var options = normalized[0]; debug('createConnection', normalized); var socket = new Socket(options); if (options.timeout) { socket.setTimeout(options.timeout); } return socket.connect(normalized); }উদাহরণ ২
// Node.js program to demonstrate the creation of socket // using agent.createConnection() method // 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: 0, 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 connection.js Succesfully created connection with agent: function toString() { [native code] } Succesfully created connection with alive agent: function toString() { [native code] }