index.js
/**
 * Creates a Discovery object. The options object is optional. Supported
 * options are:
 *   - port - Set the port the service listens on for announcements default:
 *     44201
 *   - bindAddr - bind to an address
 *   - dgramType - Either 'udp4' or 'udp6', default: 'udp4'.
 *   - timeOutInt - duration of time between timeout checks in ms. Default 1000.
 * @param {Object} [options] An optional configuration object.
 * @constructor
 */
Creates a Discovery object. The options object is optional.
Creates a Discovery object . The options object is optional .
function Discovery(options) {
    var self = this;
    if (options && !is.obj(options))
        debug('Dicovery constructor bad options argument: '+inspect(options));
    self.dgramType = (options && options.dgramType) ?
                      options.dgramType.toLowerCase() : DEFAULT_DGRAM_TYPE;
    self.reuseaddr = (options && options.reuseaddr) ? options.reuseaddr : DEFAULT_REUSE_ADDR;
    self.socket = dgram.createSocket({type: self.dgramType, reuseAddr: self.reuseaddr});
    self.port = (options && options.port) ? options.port : DEFAULT_UDP_PORT;
    self.bindAddr = (options && options.bindAddr) ? options.bindAddr :
                     undefined;
    self.socket.bind(self.port, self.bindAddr);
    self.timeOutInt = (options && options.timeOutInt) ? options.timeOutInt :
                                        DEFAULT_TIMEOUT;
    self.timeOutId = setInterval(function() { self.handleTimeOut(); },
                                                             self.timeOutInt);
    self.socket.on('listening', function() {
        self.socket.addMembership(MULTICAST_ADDRESS);
    });
    self.socket.on('message', function(message, rinfo) {
        if (message) {
            var obj = objToJson.jsonParse(message.toString());
            if (!obj) {
                debug('bad announcement: '+message.toString());
                return;
            }
            if (obj.eventName)
                self.emit(GLOBAL_EVENT_NAME, obj.eventName, obj.data);
            else
                self.handleAnnouncement(obj, rinfo);
        }
    });
}
function Discovery ( options ) { var self = this ; if ( options && ! is . obj ( options ) ) debug ( 'Dicovery constructor bad options argument: ' + inspect ( options ) ) ; self . dgramType = ( options && options . dgramType ) ? options . dgramType . toLowerCase ( ) : DEFAULT_DGRAM_TYPE ; self . reuseaddr = ( options && options . reuseaddr ) ? options . reuseaddr : DEFAULT_REUSE_ADDR ; self . socket = dgram . createSocket ( { type : self . dgramType , reuseAddr : self . reuseaddr } ) ; self . port = ( options && options . port ) ? options . port : DEFAULT_UDP_PORT ; self . bindAddr = ( options && options . bindAddr ) ? options . bindAddr : undefined ; self . socket . bind ( self . port , self . bindAddr ) ; self . timeOutInt = ( options && options . timeOutInt ) ? options . timeOutInt : DEFAULT_TIMEOUT ; self . timeOutId = setInterval ( function ( ) { self . handleTimeOut ( ) ; } , self . timeOutInt ) ; self . socket . on ( 'listening' , function ( ) { self . socket . addMembership ( MULTICAST_ADDRESS ) ; } ) ; self . socket . on ( 'message' , function ( message , rinfo ) { if ( message ) { var obj = objToJson . jsonParse ( message . toString ( ) ) ; if ( ! obj ) { debug ( 'bad announcement: ' + message . toString ( ) ) ; return ; } if ( obj . eventName ) self . emit ( GLOBAL_EVENT_NAME , obj . eventName , obj . data ) ; else self . handleAnnouncement ( obj , rinfo ) ; } } ) ; }
Creates a Discovery object.
Creates a Discovery object .
// Create a dgram socket and bind it // create an interval task to check for announcements that have timed out // listen and listen for multicast packets // handle any announcements, here we just do the formatting // the received message was either an event or an announcement