import { Socket } from "https://deno.land/std@0.145.0/node/net.ts";
This 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.
Properties
This property shows the number of characters buffered for writing. The buffer may contain strings whose length after encoding is not yet known. So this number is only an approximation of the number of bytes in the buffer.
net.Socket
has the property that socket.write()
always works. This is to
help users get up and running quickly. The computer cannot always keep up
with the amount of data that is written to a socket. The network connection
simply might be too slow. Node.js will internally queue up the data written to a
socket and send it out over the wire when it is possible.
The consequence of this internal buffering is that memory may grow.
Users who experience large or growing bufferSize
should attempt to
"throttle" the data flows in their program with socket.pause()
and socket.resume()
.
If true
,socket.connect(options[, connectListener])
was
called and has not yet finished. It will stay true
until the socket becomes
connected, then it is set to false
and the "connect"
event is emitted. Note
that the socket.connect(options[, connectListener])
callback is a listener for the "connect"
event.
The string representation of the local IP address the remote client is
connecting on. For example, in a server listening on "0.0.0.0"
, if a client
connects on "192.168.1.1"
, the value of socket.localAddress
would be"192.168.1.1"
.
The string representation of the remote IP address. For example,"74.125.127.100"
or "2001:4860:a005::68"
. Value may be undefined
if
the socket is destroyed (for example, if the client disconnected).
The string representation of the remote IP family. "IPv4"
or "IPv6"
.
The numeric representation of the remote port. For example, 80
or 21
.
Sets the socket to timeout after timeout
milliseconds of inactivity on
the socket. By default net.Socket
do not have a timeout.
When an idle timeout is triggered the socket will receive a "timeout"
event but the connection will not be severed. The user must manually call socket.end()
or socket.destroy()
to
end the connection.
import { createRequire } from "https://deno.land/std@0.145.0/node/module.ts";
const require = createRequire(import.meta.url);
const net = require("net");
const socket = new net.Socket();
socket.setTimeout(3000);
socket.on("timeout", () => {
console.log("socket timeout");
socket.end();
});
If timeout
is 0
, then the existing idle timeout is disabled.
The optional callback
parameter will be added as a one-time listener for the "timeout"
event.
Methods
Returns the bound address
, the address family
name and port
of the
socket as reported by the operating system:{ port: 12346, family: "IPv4", address: "127.0.0.1" }
Initiate a connection on a given socket.
Possible signatures:
socket.connect(options[, connectListener])
socket.connect(path[, connectListener])
forIPC
connections.socket.connect(port[, host][, connectListener])
for TCP connections.- Returns:
net.Socket
The socket itself.
This function is asynchronous. When the connection is established, the "connect"
event will be emitted. If there is a problem connecting,
instead of a "connect"
event, an "error"
event will be emitted with
the error passed to the "error"
listener.
The last parameter connectListener
, if supplied, will be added as a listener
for the "connect"
event once.
This function should only be used for reconnecting a socket after "close"
has been emitted or otherwise it may lead to undefined
behavior.
Half-closes the socket. i.e., it sends a FIN packet. It is possible the server will still send some data.
See writable.end()
for further details.
Pauses the reading of data. That is, "data"
events will not be emitted.
Useful to throttle back an upload.
Opposite of unref()
, calling ref()
on a previously unref
ed socket will_not_ let the program exit if it's the only socket left (the default behavior).
If the socket is ref
ed calling ref
again will have no effect.
Resumes reading after a call to socket.pause()
.
Enable/disable keep-alive functionality, and optionally set the initial delay before the first keepalive probe is sent on an idle socket.
Set initialDelay
(in milliseconds) to set the delay between the last
data packet received and the first keepalive probe. Setting 0
forinitialDelay
will leave the value unchanged from the default
(or previous) setting.
Enabling the keep-alive functionality will set the following socket options:
SO_KEEPALIVE=1
TCP_KEEPIDLE=initialDelay
TCP_KEEPCNT=10
TCP_KEEPINTVL=1
Enable/disable the use of Nagle's algorithm.
When a TCP connection is created, it will have Nagle's algorithm enabled.
Nagle's algorithm delays data before it is sent via the network. It attempts to optimize throughput at the expense of latency.
Passing true
for noDelay
or not passing an argument will disable Nagle's
algorithm for the socket. Passing false
for noDelay
will enable Nagle's
algorithm.
Calling unref()
on a socket will allow the program to exit if this is the only
active socket in the event system. If the socket is already unref
ed callingunref()
again will have no effect.