Object DgramSocket
dgram.Socket The object is a package that encapsulates the function of the data packet EventEmitter.
The DgramSocket instance is created by dgram.createSocket() Created. createdgram.Socket The instance does not need to use the new keyword.
Creation method:
1
2var dgram = require('dgram');
var sock = dgram.createSocket('udp4');
event
DgramSocket inherited from EventEmitter, The state change of the object and the data acceptance are all realized in the form of events.
close event
close
Events will use close()
to close a socket
trigger after. The event Once triggered, this socket
will not trigger a new message
event.
error event
When any error occurs, the error
event will be triggered.
listening event
When a socket
start of listening packet information, listening
the event will be triggered. This event will be triggered immediately after the UDP socket is created.
message event
When a new packet is socket
receive, the message
event will be triggered. msg
And rinfo
passed to the event handler as a parameter.
- 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
Static properties
defaultMaxListeners
Integer, the default global maximum number of listeners
1static Integer DgramSocket.defaultMaxListeners;
Member function
bind
This method will make dgram.SocketSpecified port
and addr
listen on packet information. When binding triggers a complete listening
event.
1
2DgramSocket.bind(Integer port = 0,
String addr = "") async;
Call parameters:
- port: Integer, specifies the port binding, if
port
not specified or is zero, the system attempts to bind a random port - addr: String, specify the binding address. If the address is not specified, the operating system will try to listen on all addresses.
This method will make dgram.SocketIn opts
specified port
and address
listen on packet information. When binding triggers a complete listening
event.
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 for sending
- address: String, specify the destination address for sending
Return result:
- Integer, Return send 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, the specified length of sending
- port: Integer, specify the destination port for sending
- address: String, specify the destination address for sending
Return result:
- Integer, Return send size
address
Returns an object containing socket address information. For UDP socket, this object will contain address, family and port attributes.
1NObject DgramSocket.address();
Return result:
- NObject, Return the object binding address
close
Close the current socket
1DgramSocket.close();
Close the current socket
1DgramSocket.close(Function callback);
Call parameters:
- callback: Function, shut down after the completion of the callback function, which is equivalent to
close
adding an event listener
getRecvBufferSize
Query socket receiving buffer size
1Integer DgramSocket.getRecvBufferSize();
Return result:
- Integer, Return query result
getSendBufferSize
Query the size of the socket sending buffer
1Integer DgramSocket.getSendBufferSize();
Return result:
- Integer, Return query result
setRecvBufferSize
Set the socket receiving buffer size
1DgramSocket.setRecvBufferSize(Integer size);
Call parameters:
- size: Integer, specify the size to be set
setSendBufferSize
Set the 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 result:
- DgramSocket, Returns the current object
unref
Allow the fibjs process to exit, and allow the fibjs process to exit during object binding
1DgramSocket DgramSocket.unref();
Return result:
- 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, specify the name of the event
- func: Function, specify event processing function
Return result:
- Object, Return the event object itself, which is convenient for chain call
Bind an event handler to the object
1Object DgramSocket.on(Object map);
Call parameters:
- map: Object, specify the event mapping relationship, the object property name will be used as the event name, and the property value will be used as the event processing function
Return result:
- Object, Return the event object itself, which is convenient for chain call
addListener
Bind an event handler to the object
1
2Object DgramSocket.addListener(String ev,
Function func);
Call parameters:
- ev: String, specify the name of the event
- func: Function, specify event processing function
Return result:
- Object, Return the event object itself, which is convenient for chain call
Bind an event handler to the object
1Object DgramSocket.addListener(Object map);
Call parameters:
- map: Object, specify the event mapping relationship, the object property name will be used as the event name, and the property value will be used as the event processing function
Return result:
- Object, Return the event object itself, which is convenient for chain call
prependListener
Bind an event handler to the start of the object
1
2Object DgramSocket.prependListener(String ev,
Function func);
Call parameters:
- ev: String, specify the name of the event
- func: Function, specify event processing function
Return result:
- Object, Return the event object itself, which is convenient for chain call
Bind an event handler to the start of the object
1Object DgramSocket.prependListener(Object map);
Call parameters:
- map: Object, specify the event mapping relationship, the object property name will be used as the event name, and the property value will be used as the event processing function
Return result:
- Object, Return the event object itself, which is convenient for chain call
once
Bind a one-time event processing function to the object, the one-time processing function will only be triggered once
1
2Object DgramSocket.once(String ev,
Function func);
Call parameters:
- ev: String, specify the name of the event
- func: Function, specify event processing function
Return result:
- Object, Return the event object itself, which is convenient for chain call
Bind a one-time event processing function to the object, the one-time processing function will only be triggered once
1Object DgramSocket.once(Object map);
Call parameters:
- map: Object, specify the event mapping relationship, the object property name will be used as the event name, and the property value will be used as the event processing function
Return result:
- Object, Return the event object itself, which is convenient for chain call
prependOnceListener
Bind an event handler to the start of the object
1
2Object DgramSocket.prependOnceListener(String ev,
Function func);
Call parameters:
- ev: String, specify the name of the event
- func: Function, specify event processing function
Return result:
- Object, Return the event object itself, which is convenient for chain call
Bind an event handler to the start of the object
1Object DgramSocket.prependOnceListener(Object map);
Call parameters:
- map: Object, specify the event mapping relationship, the object property name will be used as the event name, and the property value will be used as the event processing function
Return result:
- Object, Return the event object itself, which is convenient for chain call
off
Cancel the specified function from the object processing queue
1
2Object DgramSocket.off(String ev,
Function func);
Call parameters:
- ev: String, specify the name of the event
- func: Function, specify event processing function
Return result:
- Object, Return the event object itself, which is convenient for chain call
Cancel all functions in the object processing queue
1Object DgramSocket.off(String ev);
Call parameters:
- ev: String, specify the name of the event
Return result:
- Object, Return the event object itself, which is convenient for chain call
Cancel the specified function from the object processing queue
1Object DgramSocket.off(Object map);
Call parameters:
- map: Object, specify the event mapping relationship, the object property name is used as the event name, and the property value is used as the event processing function
Return result:
- Object, Return the event object itself, which is convenient for chain call
removeListener
Cancel the specified function from the object processing queue
1
2Object DgramSocket.removeListener(String ev,
Function func);
Call parameters:
- ev: String, specify the name of the event
- func: Function, specify event processing function
Return result:
- Object, Return the event object itself, which is convenient for chain call
Cancel all functions in the object processing queue
1Object DgramSocket.removeListener(String ev);
Call parameters:
- ev: String, specify the name of the event
Return result:
- Object, Return the event object itself, which is convenient for chain call
Cancel the specified function from the object processing queue
1Object DgramSocket.removeListener(Object map);
Call parameters:
- map: Object, specify the event mapping relationship, the object property name is used as the event name, and the property value is used as the event processing function
Return result:
- Object, Return the event object itself, which is convenient for chain call
removeAllListeners
Cancel all the listeners of all events from the object processing queue. If an event is specified, remove all the listeners of the specified event.
1Object DgramSocket.removeAllListeners(String ev);
Call parameters:
- ev: String, specify the name of the event
Return result:
- Object, Return the event object itself, which is convenient for chain call
Cancel all the listeners of all events from the object processing queue. If an event is specified, remove all the listeners of the specified event.
1Object DgramSocket.removeAllListeners(Array evs = []);
Call parameters:
- evs: Array, specify the name of the event
Return result:
- Object, Return the event object itself, which is convenient for chain call
setMaxListeners
The default limit of the number of listeners, only for compatibility
1DgramSocket.setMaxListeners(Integer n);
Call parameters:
- n: Integer, specify the number of events
getMaxListeners
Get the default limit of the number of listeners, only for compatibility
1Integer DgramSocket.getMaxListeners();
Return result:
- Integer, Return to the default limit number
listeners
Array of listeners for the specified event of the query object
1Array DgramSocket.listeners(String ev);
Call parameters:
- ev: String, specify the name of the event
Return result:
- Array, Returns an array of listeners for the specified event
listenerCount
The number of listeners for the specified event of the query object
1Integer DgramSocket.listenerCount(String ev);
Call parameters:
- ev: String, specify the name of the event
Return result:
- Integer, Returns the number of listeners for the specified event
The number of listeners for the specified event of the query object
1
2Integer DgramSocket.listenerCount(Value o,
String ev);
Call parameters:
- o: Value, specify the object of the query
- ev: String, specify the name of the event
Return result:
- Integer, Returns the number of listeners for the specified event
eventNames
Query the event name of the listener
1Array DgramSocket.eventNames();
Return result:
- Array, Returns an array of event names
emit
Trigger an event actively
1
2Boolean DgramSocket.emit(String ev,
...args);
Call parameters:
- ev: String, event name
- args: ..., event parameters, which will be passed to the event handler
Return result:
- Boolean, Return to the event trigger state, return true if there is a response event, otherwise return false
toString
Returns the string representation of the object, generally returns "[Native Object]", the object can be re-implemented according to its own characteristics
1String DgramSocket.toString();
Return result:
- String, Returns the string representation of the object
toJSON
Returns the JSON format representation of the object, generally returns a collection of readable attributes defined by the object
1Value DgramSocket.toJSON(String key = "");
Call parameters:
- key: String, unused
Return result:
- Value, Returns a value containing JSON serializable