Exports
[<Class>]
type Exports =
static member BlockList () : BlockList
static member Server (?options: ServerOpts, ?connectionListener: (Socket -> unit)) : Server
static member Server (?connectionListener: (Socket -> unit)) : Server
static member Socket (?options: SocketConstructorOpts) : Socket
static member SocketAddress (options: SocketAddressInitOptions) : SocketAddress
static member connect (path: string, ?connectionListener: (unit -> unit)) : Socket
static member connect (port: float, ?host: string, ?connectionListener: (unit -> unit)) : Socket
static member connect (options: NetConnectOpts, ?connectionListener: (unit -> unit)) : Socket
static member createConnection (path: string, ?connectionListener: (unit -> unit)) : Socket
static member createConnection (port: float, ?host: string, ?connectionListener: (unit -> unit)) : Socket
static member createConnection (options: NetConnectOpts, ?connectionListener: (unit -> unit)) : Socket
static member createServer (?options: ServerOpts, ?connectionListener: (Socket -> unit)) : Server
static member createServer (?connectionListener: (Socket -> unit)) : Server
static member getDefaultAutoSelectFamily () : bool
static member getDefaultAutoSelectFamilyAttemptTimeout () : float
static member isIP (input: string) : float
static member isIPv4 (input: string) : bool
static member isIPv6 (input: string) : bool
static member setDefaultAutoSelectFamily (value: bool) : unit
static member setDefaultAutoSelectFamilyAttemptTimeout (value: float) : unit| Assembly | Xantham.Fable.Node |
| Attributes | [<Erase>] |
The package's value exports, each bound to its import.
Methods
static member BlockList () : BlockList
static member BlockList () : BlockListThe BlockList object can be used with some network APIs to specify rules for disabling inbound or outbound access to specific IP addresses, IP ranges, or IP subnets.
@since v15.0.0, v14.18.0
static member Server (?options: ServerOpts, ?connectionListener: (Socket -> unit)) : Server
static member Server (?options: ServerOpts, ?connectionListener: (Socket -> unit)) : ServerThis class is used to create a TCP or IPC server.
@since v0.1.90
static member Server (?connectionListener: (Socket -> unit)) : Server
static member Server (?connectionListener: (Socket -> unit)) : ServerThis class is used to create a TCP or IPC server.
@since v0.1.90
static member Socket (?options: SocketConstructorOpts) : Socket
static member Socket (?options: SocketConstructorOpts) : SocketThis class is an abstraction of a TCP socket or a streaming IPC endpoint (uses named pipes on Windows, and Unix domain sockets otherwise). It is also an EventEmitter.
A net.Socket can be created by the user and used directly to interact with a server. For example, it is returned by createConnection, so the user can use it to talk to the server.
It can also be created by Node.js and passed to the user when a connection is received. For example, it is passed to the listeners of a 'connection' event emitted on a Server, so the user can use it to interact with the client.
@since v0.3.4
static member SocketAddress (options: SocketAddressInitOptions) : SocketAddress
static member SocketAddress (options: SocketAddressInitOptions) : SocketAddress@since v15.14.0, v14.18.0
static member connect (path: string, ?connectionListener: (unit -> unit)) : Socket
static member connect (path: string, ?connectionListener: (unit -> unit)) : SocketAliases to createConnection.
Possible signatures:
- connect
- connect for
IPCconnections. - connect for TCP connections.
static member connect (port: float, ?host: string, ?connectionListener: (unit -> unit)) : Socket
static member connect (port: float, ?host: string, ?connectionListener: (unit -> unit)) : SocketAliases to createConnection.
Possible signatures:
- connect
- connect for
IPCconnections. - connect for TCP connections.
static member connect (options: NetConnectOpts, ?connectionListener: (unit -> unit)) : Socket
static member connect (options: NetConnectOpts, ?connectionListener: (unit -> unit)) : SocketAliases to createConnection.
Possible signatures:
- connect
- connect for
IPCconnections. - connect for TCP connections.
static member createConnection (path: string, ?connectionListener: (unit -> unit)) : Socket
static member createConnection (path: string, ?connectionListener: (unit -> unit)) : SocketA factory function, which creates a new Socket, immediately initiates connection with socket.connect(), then returns the net.Socket that starts the connection.
When the connection is established, a 'connect' event will be emitted on the returned socket. The last parameter connectListener, if supplied, will be added as a listener for the 'connect' event once.
Possible signatures:
- createConnection
- createConnection for
IPCconnections. - createConnection for TCP connections.
The connect function is an alias to this function.
static member createConnection (port: float, ?host: string, ?connectionListener: (unit -> unit)) : Socket
static member createConnection (port: float, ?host: string, ?connectionListener: (unit -> unit)) : SocketA factory function, which creates a new Socket, immediately initiates connection with socket.connect(), then returns the net.Socket that starts the connection.
When the connection is established, a 'connect' event will be emitted on the returned socket. The last parameter connectListener, if supplied, will be added as a listener for the 'connect' event once.
Possible signatures:
- createConnection
- createConnection for
IPCconnections. - createConnection for TCP connections.
The connect function is an alias to this function.
static member createConnection (options: NetConnectOpts, ?connectionListener: (unit -> unit)) : Socket
static member createConnection (options: NetConnectOpts, ?connectionListener: (unit -> unit)) : SocketA factory function, which creates a new Socket, immediately initiates connection with socket.connect(), then returns the net.Socket that starts the connection.
When the connection is established, a 'connect' event will be emitted on the returned socket. The last parameter connectListener, if supplied, will be added as a listener for the 'connect' event once.
Possible signatures:
- createConnection
- createConnection for
IPCconnections. - createConnection for TCP connections.
The connect function is an alias to this function.
static member createServer (?options: ServerOpts, ?connectionListener: (Socket -> unit)) : Server
static member createServer (?options: ServerOpts, ?connectionListener: (Socket -> unit)) : ServerCreates a new TCP or IPC server.
If allowHalfOpen is set to true, when the other end of the socket signals the end of transmission, the server will only send back the end of transmission when socket.end() is explicitly called. For example, in the context of TCP, when a FIN packed is received, a FIN packed is sent back only when socket.end() is explicitly called. Until then the connection is half-closed (non-readable but still writable). See 'end' event and RFC 1122 (section 4.2.2.13) for more information.
If pauseOnConnect is set to true, then the socket associated with each incoming connection will be paused, and no data will be read from its handle. This allows connections to be passed between processes without any data being read by the original process. To begin reading data from a paused socket, call socket.resume().
The server can be a TCP server or an IPC server, depending on what it listen() to.
Here is an example of a TCP echo server which listens for connections on port 8124:
import net from 'node:net';
const server = net.createServer((c) => {
// 'connection' listener.
console.log('client connected');
c.on('end', () => {
console.log('client disconnected');
});
c.write('hello\r\n');
c.pipe(c);
});
server.on('error', (err) => {
throw err;
});
server.listen(8124, () => {
console.log('server bound');
});
Test this by using telnet:
telnet localhost 8124
To listen on the socket /tmp/echo.sock:
server.listen('/tmp/echo.sock', () => {
console.log('server bound');
});
Use nc to connect to a Unix domain socket server:
nc -U /tmp/echo.sock
@since v0.5.0
| Parameter | |
|---|---|
connectionListener | Automatically set as a listener for the {@link 'connection'} event. |
static member createServer (?connectionListener: (Socket -> unit)) : Server
static member createServer (?connectionListener: (Socket -> unit)) : ServerCreates a new TCP or IPC server.
If allowHalfOpen is set to true, when the other end of the socket signals the end of transmission, the server will only send back the end of transmission when socket.end() is explicitly called. For example, in the context of TCP, when a FIN packed is received, a FIN packed is sent back only when socket.end() is explicitly called. Until then the connection is half-closed (non-readable but still writable). See 'end' event and RFC 1122 (section 4.2.2.13) for more information.
If pauseOnConnect is set to true, then the socket associated with each incoming connection will be paused, and no data will be read from its handle. This allows connections to be passed between processes without any data being read by the original process. To begin reading data from a paused socket, call socket.resume().
The server can be a TCP server or an IPC server, depending on what it listen() to.
Here is an example of a TCP echo server which listens for connections on port 8124:
import net from 'node:net';
const server = net.createServer((c) => {
// 'connection' listener.
console.log('client connected');
c.on('end', () => {
console.log('client disconnected');
});
c.write('hello\r\n');
c.pipe(c);
});
server.on('error', (err) => {
throw err;
});
server.listen(8124, () => {
console.log('server bound');
});
Test this by using telnet:
telnet localhost 8124
To listen on the socket /tmp/echo.sock:
server.listen('/tmp/echo.sock', () => {
console.log('server bound');
});
Use nc to connect to a Unix domain socket server:
nc -U /tmp/echo.sock
@since v0.5.0
| Parameter | |
|---|---|
connectionListener | Automatically set as a listener for the {@link 'connection'} event. |
static member getDefaultAutoSelectFamily () : bool
static member getDefaultAutoSelectFamily () : boolGets the current default value of the autoSelectFamily option of socket.connect(options). The initial default value is true, unless the command line option--no-network-family-autoselection is provided.
@since v19.4.0
static member getDefaultAutoSelectFamilyAttemptTimeout () : float
static member getDefaultAutoSelectFamilyAttemptTimeout () : floatGets the current default value of the autoSelectFamilyAttemptTimeout option of socket.connect(options). The initial default value is 250 or the value specified via the command line option --network-family-autoselection-attempt-timeout.
@since v19.8.0, v18.8.0
Returns. The current default value of the autoSelectFamilyAttemptTimeout option.
static member isIP (input: string) : float
static member isIP (input: string) : floatReturns 6 if input is an IPv6 address. Returns 4 if input is an IPv4 address in dot-decimal notation with no leading zeroes. Otherwise, returns0.
net.isIP('::1'); // returns 6
net.isIP('127.0.0.1'); // returns 4
net.isIP('127.000.000.001'); // returns 0
net.isIP('127.0.0.1/24'); // returns 0
net.isIP('fhqwhgads'); // returns 0
@since v0.3.0
static member isIPv4 (input: string) : bool
static member isIPv4 (input: string) : boolReturns true if input is an IPv4 address in dot-decimal notation with no leading zeroes. Otherwise, returns false.
net.isIPv4('127.0.0.1'); // returns true
net.isIPv4('127.000.000.001'); // returns false
net.isIPv4('127.0.0.1/24'); // returns false
net.isIPv4('fhqwhgads'); // returns false
@since v0.3.0
static member isIPv6 (input: string) : bool
static member isIPv6 (input: string) : boolReturns true if input is an IPv6 address. Otherwise, returns false.
net.isIPv6('::1'); // returns true
net.isIPv6('fhqwhgads'); // returns false
@since v0.3.0
static member setDefaultAutoSelectFamily (value: bool) : unit
static member setDefaultAutoSelectFamily (value: bool) : unitSets the default value of the autoSelectFamily option of socket.connect(options).
@since v19.4.0
| Parameter | |
|---|---|
value | The new default value. The initial default value is true, unless the command line option --no-network-family-autoselection is provided. |
static member setDefaultAutoSelectFamilyAttemptTimeout (value: float) : unit
static member setDefaultAutoSelectFamilyAttemptTimeout (value: float) : unitSets the default value of the autoSelectFamilyAttemptTimeout option of socket.connect(options).
@since v19.8.0, v18.8.0
| Parameter | |
|---|---|
value | The new default value, which must be a positive number. If the number is less than 10, the value 10 is used instead. The initial default value is 250 or the value specified via the command line option --network-family-autoselection-attempt-timeout. |