src/js/lib/RTRemoteServer.js
/**
   * open multicast server to receive udp locate object packet and return message
   * @param {string} udpHost the multicast udp address
   * @param {number|int} udpPort the multicast udp port
   * @return {Promise<dgram>} the promise with dgram
   */
open multicast server to receive udp locate object packet and return message
@param {string} udpHost the multicast udp address
@param {number|int} udpPort the multicast udp port
@return {Promise} the promise with dgram
open multicast server to receive udp locate object packet and return message @param { string } udpHost the multicast udp address @param { number|int } udpPort the multicast udp port @return { Promise } the promise with dgram
openMulticastServer(udpHost, udpPort) {
    const udpSocketIn = dgram.createSocket({
      type: 'udp4',
      reuseAddr: true,
    });
    const udpSocketOut = dgram.createSocket('udp4');
    udpSocketOut.bind(() => { 
      udpSocketOut.setBroadcast(true);
      udpSocketOut.addMembership(udpHost);
    });
    return new Promise((resolve, reject) => {
      udpSocketIn.on('error', (err) => {
        logger.error(`multicast socket in error:\n${err.stack}`);
        reject(new RTException(err.message));
        udpSocketIn.close();
      });
      udpSocketIn.on('message', (buffer) => {
        const msg = RTRemoteSerializer.fromBuffer(buffer);
        const replyToURL = new URL(msg[RTConst.REPLY_TO]);
        const objectId = msg[RTConst.OBJECT_ID_KEY];
        if (registeredObjectMap[objectId]) {
          const locateMsg = RTMessageHelper.newLocateResponse(
            `tcp://${this.rpcHost}:${this.rpcPort}`,
            objectId, msg[RTConst.SENDER_ID], msg[RTConst.CORRELATION_KEY],
          );
          udpSocketOut.send(
            RTRemoteSerializer.toBuffer(locateMsg),
            replyToURL.port, replyToURL.hostname, (err) => {
              if (err) { 
                logger.error(`send locate response to ${replyToURL.hostname}:${replyToURL.port} failed`);
                logger.error(err.stack);
              }
            },
          );
        } else {
          logger.warn(`client want search object ${objectId}, but not found in server ${this.serverName}.`);
        }
      });
      udpSocketIn.on('listening', () => {
        const address = udpSocketIn.address();
        logger.debug(`server bind multicast socket succeed, ${address.address}:${address.port}`);
        resolve(udpSocketIn);
      });
      udpSocketIn.bind({ port: udpPort }, () => {
        udpSocketIn.setBroadcast(true);
        udpSocketIn.addMembership(udpHost); 
      });
    });
  }
openMulticastServer ( udpHost , udpPort ) { const udpSocketIn = dgram . createSocket ( { type : 'udp4' , reuseAddr : true , } ) ; const udpSocketOut = dgram . createSocket ( 'udp4' ) ; udpSocketOut . bind ( ( ) => { udpSocketOut . setBroadcast ( true ) ; udpSocketOut . addMembership ( udpHost ) ; } ) ; return new Promise ( ( resolve , reject ) => { udpSocketIn . on ( 'error' , ( err ) => { logger . error ( ` \n ${ err . stack } ` ) ; reject ( new RTException ( err . message ) ) ; udpSocketIn . close ( ) ; } ) ; udpSocketIn . on ( 'message' , ( buffer ) => { const msg = RTRemoteSerializer . fromBuffer ( buffer ) ; const replyToURL = new URL ( msg [ RTConst . REPLY_TO ] ) ; const objectId = msg [ RTConst . OBJECT_ID_KEY ] ; if ( registeredObjectMap [ objectId ] ) { const locateMsg = RTMessageHelper . newLocateResponse ( ` ${ this . rpcHost } ${ this . rpcPort } ` , objectId , msg [ RTConst . SENDER_ID ] , msg [ RTConst . CORRELATION_KEY ] , ) ; udpSocketOut . send ( RTRemoteSerializer . toBuffer ( locateMsg ) , replyToURL . port , replyToURL . hostname , ( err ) => { if ( err ) { logger . error ( ` ${ replyToURL . hostname } ${ replyToURL . port } ` ) ; logger . error ( err . stack ) ; } } , ) ; } else { logger . warn ( ` ${ objectId } ${ this . serverName } ` ) ; } } ) ; udpSocketIn . on ( 'listening' , ( ) => { const address = udpSocketIn . address ( ) ; logger . debug ( ` ${ address . address } ${ address . port } ` ) ; resolve ( udpSocketIn ) ; } ) ; udpSocketIn . bind ( { port : udpPort } , ( ) => { udpSocketIn . setBroadcast ( true ) ; udpSocketIn . addMembership ( udpHost ) ; } ) ; } ) ; }
open multicast server to receive udp locate object packet and return message
 @param {string} udpHost the multicast udp address
 @param {number|int} udpPort the multicast udp port
 @return {Promise<dgram>} the promise with dgram
open multicast server to receive udp locate object packet and return message @param { string } udpHost the multicast udp address @param { number|int } udpPort the multicast udp port @return { Promise<dgram > } the promise with dgram
// create channel to send located message // send failed, only log this error, no need do other actions // join to multicast channel