1.0.2 • Published 6 years ago

simpleudpdiscovery v1.0.2

Weekly downloads
2
License
UNLICENSED
Repository
-
Last release
6 years ago

Simple UDP discovery package

The package exports two constructors function, Sender and Receiver.

By default the Receiver binds to port 8888 and the Sender bind to port 9999.

The Sender constructor takes 4 optional parameters:

  • bindindPort (default to 9999)
  • remoteHostPort (default to 8888)
  • remoteHostAddress (default to 255.255.255.255)
  • interval (default to 1000ms), this parameter is used to determine the interval at which the discovery packets are send

The Receiver constructor takes one optional parameter:

  • bindingPort (default to 8888)

The Sender object has two methods:

  • startUDP(message) which starts sending udp packages containing the message you provide
  • stopUDP which stops the udp discovery

The Receiver object has two methods:

  • startListening which starts listening on the port you specified
  • stopListening which stops listening

Example:

const Receiver = require("./main").Receiver;
const Sender = require("./main").Sender;

var rec = new Receiver();
var server = rec.startListening();

server.on('message', (msg,rinfo) => {
    console.log("received message: " + msg);
})
.on('error', (err) =>{
    console.log("An error has occured");
});


var sender = new Sender(9999);
sender.startUDP("test message");
setTimeout(() =>{
    sender.stopUDP();
    rec.stopListening();
    clearTimeout(this);
}, 10000);

The server object is used to listen to events that you receive when using discovery.