net.createConnection(options[, connectListener])
添加於:v0.1.90
參數
options<Object> 必需。將傳遞給調用和new net.Socket([options])方法。socket.connect(options[, connectListener])connectListener<Function>函數的公共參數。如果提供,將在返回的套接字上添加一次作為net.createConnection()事件的偵聽器。'connect'- 返回: <net.Socket> 新創建的用於啟動連接的套接字。
有關可用選項,請參閱 和 new net.Socket([options]) 。socket.connect(options[, connectListener])
其他選項:
timeout<number> 如果設置,將用於在創建套接字之後但在開始連接之前調用。socket.setTimeout(timeout)
以下是 部分中說明的回顯服務器客戶端示例:net.createServer()
const net = require('node:net');
const client = net.createConnection({ port: 8124 }, () => {
// 'connect' listener.
console.log('connected to server!');
client.write('world!\r\n');
});
client.on('data', (data) => {
console.log(data.toString());
client.end();
});
client.on('end', () => {
console.log('disconnected from server');
});
要連接套接字 /tmp/echo.sock :
const client = net.createConnection({ path: '/tmp/echo.sock' });
相關用法
- Node.js net.createServer([options][, connectionListener])用法及代碼示例
- Node.js net.isIP(input)用法及代碼示例
- Node.js net.isIPv6(input)用法及代碼示例
- Node.js net.Server.address()用法及代碼示例
- Node.js net.Server.listen()用法及代碼示例
- Node.js net.Socket.setTimeout(timeout[, callback])用法及代碼示例
- Node.js net.BlockList.check(address[, type])用法及代碼示例
- Node.js net.isIPv4(input)用法及代碼示例
- Node.js new assert.AssertionError(options)用法及代碼示例
- Node.js new AsyncResource(type[, options])用法及代碼示例
- Node.js new stream.Duplex(options)用法及代碼示例
- Node.js new stream.Readable([options])用法及代碼示例
- Node.js new Console(options)用法及代碼示例
- Node.js new URLSearchParams(obj)用法及代碼示例
- Node.js new crypto.Certificate()用法及代碼示例
- Node.js new stream.Writable([options])用法及代碼示例
- Node.js new URLSearchParams(iterable)用法及代碼示例
- Node.js new Agent([options])用法及代碼示例
- Node.js new vm.SourceTextModule(code[, options])用法及代碼示例
- Node.js new stream.Transform([options])用法及代碼示例
- Node.js new PerformanceObserver(callback)用法及代碼示例
- Node.js new URL(input[, base])用法及代碼示例
- Node.js new URLSearchParams(string)用法及代碼示例
- Node.js new assert.CallTracker()用法及代碼示例
- Node.js ServerHttp2Stream http2stream.pushStream(headers[, options], callback)用法及代碼示例
注:本文由純淨天空篩選整理自nodejs.org大神的英文原創作品 net.createConnection(options[, connectListener])。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。
