An enhanced Node.js dgram module.
This module extends the functionality of the internal
dgram. By default,
emit() method in dgram module acts just same as
EventEmitter does, but this module change the way of how they bind a
listener and emit an event.
This module allows you bind custom events, and instead of emitting them by the current object, the event will be emitted by the remote peer, just like how socket.io does, and build communication in a more easier way.
const createServer createClient = ;let addr = "udp://127.0.0.1:12345";var server = ;var client = ;server;client; // the client binds a random port and wait for reply.client;
This class extends dgram.Socket, provides an easier way to communicate between two remote peers.
Sets what events are reserved by the module (or your module) and should be avoid using for remote emitting. By default, these events is reserved:
closetriggered when the socket is closed.
errortriggered when any error occurred.
bind()method is called.
messagetriggered when receiving any message.
Socket.prototype.on(event: string, listener: Function): Socket
If the event is reserved, then it acts just as talked above. But if the event is customized, then the listener function will be called when the remote peer emits the event. The signature of the listener function is:
(msg: any, rinfo: dgram.RemoteInfo) => void
msgThe message needed to be sent.
rinfoRemote information contains:
address: stringthe address of the remote peer.
port: numberbinding port of the remote peer.
family: "IPv4" | "IPv6"
Socket.prototype.emit(event: string, msg: any): boolean
Apart from reserved events, you can emit any customized event to the remote
peer, and sends data when the event fires. Be aware, the number of
should be equal to the one specified by the remote peer
Sets the receiver information of the remote peer, valid signatures are:
to(addr: string, port: number)e.g
You can call this method several times to set multiple receivers, but after
emit() is called, the receivers will be set to empty.
The same as dgram.createSocket(), only it returns the
Socket created by
createServer(addr: string, callback?: () => void)
Creates a UDP server according to the given address. This is a short-hand for
addr must contain a protocol, could be either
Creates a UDP client ready to the given server. This is a short-hand for
createClient() is the same and should be the same as the one
The Data Frame
For efficiency concerns, this module uses JSON to encode and decode data into and from buffer.