Object built-in object

Object DgramSocket

dgram.Socket object is a function of encapsulating a packet functionality EventEmitter .

The DgramSocket instance is created by dgram.createSocket (). dgram.Socket no need to use the new keyword to create dgram.Socket instance of dgram.Socket .

Creation method:

1 2
var dgram = require('dgram'); var sock = dgram.createSocket('udp4');

event

DgramSocket inherits from EventEmitter . The state change of the object and the data reception are all implemented in the form of events.

close event

close event will be triggered after closing a socket using close() . Once this event is triggered, no new message event will be triggered on this socket .

error event

When any error occurs, the error event will be triggered.

listening event

When a socket starts listening for packet information, the listening event will be triggered. This event will be triggered immediately after the UDP socket is created.

message event

When a new packet is received by the socket , the message event will be triggered. msg and rinfo will be passed as parameters to the event handler.

  • msg : Buffer , message
  • 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

1
static Integer DgramSocket.defaultMaxListeners;

Member function

bind

This method will make dgram.Socket monitor the packet information on the specified port and addr . When the binding is complete, a listening event is triggered.

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

Call parameters:

  • port : Integer, specify the binding port, if port not specified or is 0, the operating system will try to bind a random port
  • addr : String, specify the binding address. If address is not specified, the operating system will try to listen on all addresses.

This method will make dgram.Socket monitor the packet information on the port and address specified by opts . When the binding is complete, a listening event is triggered.

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 to send

Return result:

  • Integer , returns the sending 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 to send

Return result:

  • Integer , returns the sending 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 , returns 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, the callback function after closing, it is equivalent to adding a listener to the close event

getRecvBufferSize

Query socket receiving buffer size

1
Integer DgramSocket.getRecvBufferSize();

Return result:

  • Integer , return query result

getSendBufferSize

Query socket send buffer size

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 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 , return 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 , return 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 , returns the event object itself, easy to 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 the event name, and the property value will be the event processing function

Return result:

  • Object , returns the event object itself, easy to 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 , returns the event object itself, easy to 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 the event name, and the property value will be the event processing function

Return result:

  • Object , returns the event object itself, easy to chain call

prependListener

Bind an event handler to the object start

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 , returns the event object itself, easy to chain call

Bind an event handler to the object start

1
Object DgramSocket.prependListener(Object map);

Call parameters:

  • map : Object, specify the event mapping relationship, the object property name will be the event name, and the property value will be the event processing function

Return result:

  • Object , returns the event object itself, easy to 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 , returns the event object itself, easy to 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 the event name, and the property value will be the event processing function

Return result:

  • Object , returns the event object itself, easy to chain call

prependOnceListener

Bind an event handler to the object start

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 , returns the event object itself, easy to chain call

Bind an event handler to the object start

1
Object DgramSocket.prependOnceListener(Object map);

Call parameters:

  • map : Object, specify the event mapping relationship, the object property name will be the event name, and the property value will be the event processing function

Return result:

  • Object , returns the event object itself, easy to 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 , returns the event object itself, easy to 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 , returns the event object itself, easy to 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 , returns the event object itself, easy to 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 , returns the event object itself, easy to 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 , returns the event object itself, easy to 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 , returns the event object itself, easy to chain call

removeAllListeners

Cancel all listeners of all events from the object processing queue. If an event is specified, remove all listeners of the specified event.

1
Object DgramSocket.removeAllListeners(Array evs = []);

Call parameters:

  • evs : Array, specify the name of the event

Return result:

  • Object , returns the event object itself, easy to 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 , returns 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

eventNames

Query listener event name

1
Array DgramSocket.eventNames();

Return result:

  • Array , returns an array of event names

emit

Actively trigger an event

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 , 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 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