This class is used to create a TCP or local server.
Variables
read onlyconnections:Null<Int>
The number of concurrent connections on the server.
This becomes null when sending a socket to a child with child_process.fork().
To poll forks and get current number of active connections use asynchronous getConnections
instead.
maxConnections:Int
Set this property to reject connections when the server's connection count gets high. It is not recommended to use this option once a socket has been sent to a child with child_process.fork().
Methods
address():SocketAdress
Returns the bound address, the address family name and port of the server as reported by the operating system. Useful to find which port was assigned when giving getting an OS-assigned address.
close(?callback:() ‑> Void):Void
close(callback:Error ‑> Void):Void
Stops the server from accepting new connections and keeps existing connections. This function is asynchronous, the server is finally closed when all connections are ended and the server emits a 'close' event.
The optional callback will be called once the 'close' event occurs. Unlike that event, it will be called with an Error as its only argument if the server was not open when it was closed.
getConnections(callback:(Error, Int) ‑> Void):Void
Asynchronously get the number of concurrent connections on the server. Works when sockets were sent to forks.
listen(options:EitherType<ServerListenOptionsTcp, ServerListenOptionsUnix>, ?callback:() ‑> Void):Void
listen(path:String, ?callback:() ‑> Void):Void
listen(handle:EitherType<Dynamic, {fd:Int}>, ?callback:() ‑> Void):Void
listen(port:Int, ?callback:() ‑> Void):Void
listen(port:Int, backlog:Int, ?callback:() ‑> Void):Void
listen(port:Int, hostname:String, ?callback:() ‑> Void):Void
listen(port:Int, hostname:String, backlog:Int, ?callback:() ‑> Void):Void
Begin accepting connections on the specified port
and hostname
.
If the hostname
is omitted, the server will accept connections on any IPv6 address (::) when IPv6 is available,
or any IPv4 address (0.0.0.0) otherwise.
A port
value of zero will assign a random port.
backlog
is the maximum length of the queue of pending connections. The actual length will be determined
by your OS through sysctl settings such as tcp_max_syn_backlog and somaxconn on linux.
The default value of this parameter is 511 (not 512).
When path
is provided, start a local socket server listening for connections on the given path.
When handle
is provided, it should be either a server or socket (anything with an underlying _handle
member),
or a {fd:
This function is asynchronous. When the server has been bound, 'listening' event will be emitted.
The last parameter callback
will be added as an listener for the 'listening' event.