Encapsulates the datagram functionality.
It should be created via Dgram.createSocket
.
Methods
addMembership(multicastAddress:String, ?multicastInterface:String):Void
Tells the kernel to join a multicast group with IP_ADD_MEMBERSHIP socket option.
If multicastInterface
is not specified, the OS will try to add membership to all valid interfaces.
bind(?callback:() ‑> Void):Void
bind(options:SocketBindOptions, ?callback:() ‑> Void):Void
bind(port:Int, address:String, ?callback:() ‑> Void):Void
bind(port:Int, ?callback:() ‑> Void):Void
Listen for datagrams on a named port
and optional address
.
If port
is not specified, the OS will try to bind to a random port.
If address
is not specified, the OS will try to listen on all addresses.
After binding is done, a "listening" event is emitted and the callback
(if specified) is called.
Specifying both a "listening" event listener and callback
is not harmful but not very useful.
A bound datagram socket keeps the node process running to receive datagrams.
If binding fails, an "error" event is generated. In rare case (e.g. binding a closed socket),
an Error
may be thrown by this method.
close(?callback:() ‑> Void):Void
Close the underlying socket and stop listening for data on it.
If a callback
is provided, it is added as a listener for the 'close' event.
dropMembership(multicastAddress:String, ?multicastInterface:String):Void
Opposite of addMembership
- tells the kernel to leave a multicast group with IP_DROP_MEMBERSHIP socket option.
This is automatically called by the kernel when the socket is closed or process terminates,
so most apps will never need to call this.
If multicastInterface
is not specified, the OS will try to drop membership to all valid interfaces.
ref():Void
Opposite of unref
, calling ref
on a previously unref
d socket will not let
the program exit if it's the only socket left (the default behavior).
If the socket is ref
d calling ref
again will have no effect.
send(buf:Buffer, offset:Int, length:Int, port:Int, address:String, ?callback:(Error, Int) ‑> Void):Void
The destination port
and address
must be specified.
A string may be supplied for the address
parameter, and it will be resolved with DNS.
If the address
is omitted or is an empty string, '0.0.0.0' or '::0' is used instead.
Depending on the network configuration, those defaults may or may not work; it's best to be
explicit about the destination address.
If the socket has not been previously bound with a call to bind
, it gets assigned a random
port number and is bound to the "all interfaces" address ('0.0.0.0' for udp4 sockets, '::0' for udp6 sockets.)
An optional callback
may be specified to detect DNS errors or for determining when it's safe
to reuse the buf object. Note that DNS lookups delay the time to send for at least one tick.
The only way to know for sure that the datagram has been sent is by using a callback
.
setBroadcast(flag:Bool):Void
Sets or clears the SO_BROADCAST socket option. When this option is set, UDP packets may be sent to a local interface's broadcast address.
setMulticastLoopback(flag:Bool):Void
Sets or clears the IP_MULTICAST_LOOP socket option. When this option is set, multicast packets will also be received on the local interface.
setMulticastTTL(ttl:Int):Void
Sets the IP_MULTICAST_TTL socket option. TTL stands for "Time to Live," but in this context it specifies the number of IP hops that a packet is allowed to go through, specifically for multicast traffic. Each router or gateway that forwards a packet decrements the TTL. If the TTL is decremented to 0 by a router, it will not be forwarded.
The argument to setMulticastTTL
is a number of hops between 0 and 255. The default on most systems is 1.
setTTL(ttl:Int):Void
Sets the IP_TTL socket option. TTL stands for "Time to Live," but in this context it specifies the number of IP hops that a packet is allowed to go through. Each router or gateway that forwards a packet decrements the TTL. If the TTL is decremented to 0 by a router, it will not be forwarded. Changing TTL values is typically done for network probes or when multicasting.
The argument to setTTL
is a number of hops between 1 and 255. The default on most systems is 64.