ObjectDgramSocket
dgram.SocketThe object is a package function that encapsulates the functionEventEmitter.
DgramSocket instances are provided bydgram.createSocket() created. createdgram.SocketInstances do not need to use the new keyword.
Creation method:
1
2var dgram = require('dgram');
var sock = dgram.createSocket('udp4');
event
DgramSocket inherits fromEventEmitter, object state changes and data reception are all implemented in the form of events.
close event
close
The event will be triggered after close()
closing one using socket
. Once this event is triggered, socket
no new message
events will be triggered on this page.
error event
When any error occurs, error
the event will be triggered.
listening event
When one socket
starts listening for packet information, listening
the event will be triggered. This event is triggered immediately after the UDP socket is created.
message event
The event is triggered socket
when a new packet is received. and will be passed as parameters to the event handler function.message
msg
rinfo
- msg:Buffer,information
- rinfo: Object, remote address information
- address: string, sender address
- family: string, address type ('IPv4' or 'IPv6')
- port: number, sender port
- size: number, message size
inheritance relationship
static properties
defaultMaxListeners
Integer, the default global maximum number of listeners
1static Integer DgramSocket.defaultMaxListeners;
member function
bind
This method will makedgram.SocketListen for packet information on the specified port
and . addr
An event is fired when the binding is complete listening
.
1
2DgramSocket.bind(Integer port = 0,
String addr = "") async;
Call parameters:
- port: Integer, specifies the binding port. If it
port
is not specified or is 0, the operating system will try to bind a random port. - addr: String, specifies the binding address. If address is not specified, the operating system will try to listen on all addresses.
This method will makedgram.SocketListen for packet information on the opts
specified port
and . address
An event is fired when the binding is complete listening
.
1DgramSocket.bind(Object opts) async;
Call parameters:
- opts: Object, specify binding parameters
send
Send a packet on the socket
1
2
3Integer DgramSocket.send(Buffer msg,
Integer port,
String address = "") async;
Call parameters:
- msg:Buffer, specify the data to be sent
- port: Integer, specify the destination port to send
- address: String, specify the destination address to send
Return results:
- Integer, return sending size
Send a packet on the socket
1
2
3
4
5Integer DgramSocket.send(Buffer msg,
Integer offset,
Integer length,
Integer port,
String address = "") async;
Call parameters:
- msg:Buffer, specify the data to be sent
- offset: Integer, start sending from the specified offset
- length: Integer, to send the specified length
- port: Integer, specify the destination port to send
- address: String, specify the destination address to send
Return results:
- Integer, return sending size
address
Returns an object containing socket address information. For UDP sockets, the object will contain address, family, and port properties.
1NObject DgramSocket.address();
Return results:
- NObject, returns the object binding address
close
Close the current socket
1DgramSocket.close();
Close the current socket
1DgramSocket.close(Function callback);
Call parameters:
- callback: Function, the callback function after the shutdown is completed, which is equivalent to
close
adding a listener for the event
getRecvBufferSize
Query the socket receive buffer size
1Integer DgramSocket.getRecvBufferSize();
Return results:
- Integer, return query results
getSendBufferSize
Query the socket send buffer size
1Integer DgramSocket.getSendBufferSize();
Return results:
- Integer, return query results
addMembership
Join the multicast group at the given multicastAddress and multicastInterface using the IP_ADD_MEMBERSHIP socket option. If the multicastInterface parameter is not specified, the operating system selects an interface and adds membership to it. To add membership to each available interface, call addMembership multiple times, once for each interface.
1
2DgramSocket.addMembership(String multicastAddress,
String multicastInterface = "");
Call parameters:
- multicastAddress: String, specifies the multicast group address to join
- multicastInterface: String, specifies the multicast group interface to join
dropMembership
Use the IP_DROP_MEMBERSHIP socket option to leave the multicast group at multicastAddress. The kernel automatically calls this method when the socket is closed or the process terminates, so most applications will never have a reason to call this method.
1
2DgramSocket.dropMembership(String multicastAddress,
String multicastInterface = "");
Call parameters:
- multicastAddress: String, specifies the multicast group address to be deleted
- multicastInterface: String, specifies the multicast group interface to be deleted
setMulticastTTL
Set IP_MULTICAST_TTL socket option
1DgramSocket.setMulticastTTL(Integer ttl);
Call parameters:
- ttl: Integer, specifies the ttl to be set, the ttl parameter can be between 0 and 255. The default value on most systems is 1.
setRecvBufferSize
Set socket receive buffer size
1DgramSocket.setRecvBufferSize(Integer size);
Call parameters:
- size: Integer, specify the size to be set
setSendBufferSize
Set socket send buffer size
1DgramSocket.setSendBufferSize(Integer size);
Call parameters:
- size: Integer, specify the size to be set
setBroadcast
Set or clear the SO_BROADCAST socket option
1DgramSocket.setBroadcast(Boolean flag);
Call parameters:
- flag: Boolean, when set to true, UDP packets will be sent to the broadcast address of a local interface
ref
Keep the fibjs process from exiting and prevent the fibjs process from exiting during object binding
1DgramSocket DgramSocket.ref();
Return results:
- DgramSocket, returns the current object
unref
Allow the fibjs process to exit. Allow the fibjs process to exit during object binding.
1DgramSocket DgramSocket.unref();
Return results:
- DgramSocket, returns the current object
on
Bind an event handler to the object
1
2Object DgramSocket.on(String ev,
Function func);
Call parameters:
- ev: String, specifies the name of the event
- func: Function, specify the event processing function
Return results:
- Object, returns the event object itself to facilitate chain calls
Bind an event handler to the object
1Object DgramSocket.on(Object map);
Call parameters:
- map: Object, specifies the event mapping relationship, the object attribute name will be used as the event name, and the value of the attribute will be used as the event processing function
Return results:
- Object, returns the event object itself to facilitate chain calls
addListener
Bind an event handler to the object
1
2Object DgramSocket.addListener(String ev,
Function func);
Call parameters:
- ev: String, specifies the name of the event
- func: Function, specify the event processing function
Return results:
- Object, returns the event object itself to facilitate chain calls
Bind an event handler to the object
1Object DgramSocket.addListener(Object map);
Call parameters:
- map: Object, specifies the event mapping relationship, the object attribute name will be used as the event name, and the value of the attribute will be used as the event processing function
Return results:
- Object, returns the event object itself to facilitate chain calls
prependListener
Bind an event handler to the object's origin
1
2Object DgramSocket.prependListener(String ev,
Function func);
Call parameters:
- ev: String, specifies the name of the event
- func: Function, specify the event processing function
Return results:
- Object, returns the event object itself to facilitate chain calls
Bind an event handler to the object's origin
1Object DgramSocket.prependListener(Object map);
Call parameters:
- map: Object, specifies the event mapping relationship, the object attribute name will be used as the event name, and the value of the attribute will be used as the event processing function
Return results:
- Object, returns the event object itself to facilitate chain calls
once
Bind a one-time event handler to the object. The one-time handler will only be triggered once.
1
2Object DgramSocket.once(String ev,
Function func);
Call parameters:
- ev: String, specifies the name of the event
- func: Function, specify the event processing function
Return results:
- Object, returns the event object itself to facilitate chain calls
Bind a one-time event handler to the object. The one-time handler will only be triggered once.
1Object DgramSocket.once(Object map);
Call parameters:
- map: Object, specifies the event mapping relationship, the object attribute name will be used as the event name, and the value of the attribute will be used as the event processing function
Return results:
- Object, returns the event object itself to facilitate chain calls
prependOnceListener
Bind an event handler to the object's origin
1
2Object DgramSocket.prependOnceListener(String ev,
Function func);
Call parameters:
- ev: String, specifies the name of the event
- func: Function, specify the event processing function
Return results:
- Object, returns the event object itself to facilitate chain calls
Bind an event handler to the object's origin
1Object DgramSocket.prependOnceListener(Object map);
Call parameters:
- map: Object, specifies the event mapping relationship, the object attribute name will be used as the event name, and the value of the attribute will be used as the event processing function
Return results:
- Object, returns the event object itself to facilitate chain calls
off
Unassign a function from the object processing queue
1
2Object DgramSocket.off(String ev,
Function func);
Call parameters:
- ev: String, specifies the name of the event
- func: Function, specify the event processing function
Return results:
- Object, returns the event object itself to facilitate chain calls
Cancel all functions in the object processing queue
1Object DgramSocket.off(String ev);
Call parameters:
- ev: String, specifies the name of the event
Return results:
- Object, returns the event object itself to facilitate chain calls
Unassign a function from the object processing queue
1Object DgramSocket.off(Object map);
Call parameters:
- map: Object, specifies the event mapping relationship, the object attribute name is used as the event name, and the value of the attribute is used as the event processing function
Return results:
- Object, returns the event object itself to facilitate chain calls
removeListener
Unassign a function from the object processing queue
1
2Object DgramSocket.removeListener(String ev,
Function func);
Call parameters:
- ev: String, specifies the name of the event
- func: Function, specify the event processing function
Return results:
- Object, returns the event object itself to facilitate chain calls
Cancel all functions in the object processing queue
1Object DgramSocket.removeListener(String ev);
Call parameters:
- ev: String, specifies the name of the event
Return results:
- Object, returns the event object itself to facilitate chain calls
Unassign a function from the object processing queue
1Object DgramSocket.removeListener(Object map);
Call parameters:
- map: Object, specifies the event mapping relationship, the object attribute name is used as the event name, and the value of the attribute is used as the event processing function
Return results:
- Object, returns the event object itself to facilitate chain calls
removeAllListeners
Cancels all listeners for all events from the object's processing queue. If an event is specified, removes all listeners for the specified event.
1Object DgramSocket.removeAllListeners(String ev);
Call parameters:
- ev: String, specifies the name of the event
Return results:
- Object, returns the event object itself to facilitate chain calls
Cancels all listeners for all events from the object's processing queue. If an event is specified, removes all listeners for the specified event.
1Object DgramSocket.removeAllListeners(Array evs = []);
Call parameters:
- evs: Array, specify the name of the event
Return results:
- Object, returns the event object itself to facilitate chain calls
setMaxListeners
The default limit on the number of listeners, for compatibility only
1DgramSocket.setMaxListeners(Integer n);
Call parameters:
- n: Integer, specify the number of events
getMaxListeners
Gets the default limit number of listeners, for compatibility only
1Integer DgramSocket.getMaxListeners();
Return results:
- Integer, returns the default limit quantity
listeners
Query the listener array for the specified event of the object
1Array DgramSocket.listeners(String ev);
Call parameters:
- ev: String, specifies the name of the event
Return results:
- Array, returns the listener array for the specified event
listenerCount
Query the number of listeners for the specified event of the object
1Integer DgramSocket.listenerCount(String ev);
Call parameters:
- ev: String, specifies the name of the event
Return results:
- Integer, returns the number of listeners for the specified event
Query the number of listeners for the specified event of the object
1
2Integer DgramSocket.listenerCount(Value o,
String ev);
Call parameters:
- o: Value, specifies the object of the query
- ev: String, specifies the name of the event
Return results:
- Integer, returns the number of listeners for the specified event
eventNames
Query listener event name
1Array DgramSocket.eventNames();
Return results:
- Array, returns an array of event names
emit
Actively trigger an event
1
2Boolean DgramSocket.emit(String ev,
...args);
Call parameters:
- ev: String, event name
- args: ..., event parameters will be passed to the event processing function
Return results:
- Boolean, returns the event trigger status, returns true if there is a response event, otherwise returns false
toString
Returns the string representation of the object. Generally, "[Native Object]" is returned. The object can be re-implemented according to its own characteristics.
1String DgramSocket.toString();
Return results:
- String, returns the string representation of the object
toJSON
Returns a JSON format representation of the object, generally returning a collection of readable properties defined by the object.
1Value DgramSocket.toJSON(String key = "");
Call parameters:
- key: String, not used
Return results:
- Value, returns a value containing JSON serializable