Object built-in object

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 2
var 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

closeEvents will use close()to close a sockettrigger after. The event Once triggered, this socketwill not trigger a new messageevent.

error event

When any error occurs, the errorevent will be triggered.

listening event

When a socketstart of listening packet information, listeningthe event will be triggered. This event will be triggered immediately after the UDP socket is created.

message event

When a new packet is socketreceive, the messageevent will be triggered. msgAnd rinfopassed 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

#lineWidth: 1.5 #font: Helvetica,sans-Serif #fontSize: 10 #leading: 1.6 #.this: fill=lightgray #.class: fill=white [<class>object|toString();toJSON()] [<class>EventEmitter|new EventEmitter()|EventEmitter|defaultMaxListeners|on();addListener();prependListener();once();prependOnceListener();off();removeListener();removeAllListeners();setMaxListeners();getMaxListeners();listeners();listenerCount();eventNames();emit()] [<this>DgramSocket|bind();send();address();close();getRecvBufferSize();getSendBufferSize();setRecvBufferSize();setSendBufferSize();setBroadcast();ref();unref()] [object] <:- [EventEmitter] [EventEmitter] <:- [DgramSocket] EventEmitter new EventEmitter() EventEmitter defaultMaxListeners on() addListener() prependListener() once() prependOnceListener() off() removeListener() removeAllListeners() setMaxListeners() getMaxListeners() listeners() listenerCount() eventNames() emit() object toString() toJSON() DgramSocket bind() send() address() close() getRecvBufferSize() getSendBufferSize() setRecvBufferSize() setSendBufferSize() setBroadcast() ref() unref()

Static properties

defaultMaxListeners

Integer, the default global maximum number of listeners

1
static Integer DgramSocket.defaultMaxListeners;

Member function

bind

This method will make dgram.SocketSpecified portand addrlisten on packet information. When binding triggers a complete listeningevent.

1 2
DgramSocket.bind(Integer port = 0, String addr = "") async;

Call parameters:

  • port: Integer, specifies the port binding, if portnot 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 optsspecified portand addresslisten on packet information. When binding triggers a complete listeningevent.

1
DgramSocket.bind(Object opts) async;

Call parameters:

  • opts: Object, specify binding parameters

send

Send a packet on the socket

1 2 3
Integer 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 5
Integer 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.

1
NObject DgramSocket.address();

Return result:

  • NObject, Return the object binding address

close

Close the current socket

1
DgramSocket.close();

Close the current socket

1
DgramSocket.close(Function callback);

Call parameters:

  • callback: Function, shut down after the completion of the callback function, which is equivalent to closeadding an event listener

getRecvBufferSize

Query socket receiving buffer size

1
Integer DgramSocket.getRecvBufferSize();

Return result:

  • Integer, Return query result

getSendBufferSize

Query the size of the socket sending buffer

1
Integer DgramSocket.getSendBufferSize();

Return result:

  • Integer, Return query result

setRecvBufferSize

Set the socket receiving buffer size

1
DgramSocket.setRecvBufferSize(Integer size);

Call parameters:

  • size: Integer, specify the size to be set

setSendBufferSize

Set the socket send buffer size

1
DgramSocket.setSendBufferSize(Integer size);

Call parameters:

  • size: Integer, specify the size to be set

setBroadcast

Set or clear the SO_BROADCAST socket option

1
DgramSocket.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

1
DgramSocket 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

1
DgramSocket DgramSocket.unref();

Return result:

  • DgramSocket, Returns the current object

on

Bind an event handler to the object

1 2
Object 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

1
Object 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 2
Object 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

1
Object 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 2
Object 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

1
Object 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 2
Object 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

1
Object 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 2
Object 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

1
Object 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 2
Object 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

1
Object 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

1
Object 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 2
Object 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

1
Object 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

1
Object 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.

1
Object 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.

1
Object 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

1
DgramSocket.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

1
Integer DgramSocket.getMaxListeners();

Return result:

  • Integer, Return to the default limit number

listeners

Array of listeners for the specified event of the query object

1
Array 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

1
Integer 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 2
Integer 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

1
Array DgramSocket.eventNames();

Return result:

  • Array, Returns an array of event names

emit

Trigger an event actively

1 2
Boolean 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

1
String 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

1
Value DgramSocket.toJSON(String key = "");

Call parameters:

  • key: String, unused

Return result:

  • Value, Returns a value containing JSON serializable