react-native-zeroconf
Version:
A Zeroconf discovery utility for react-native
2 lines (1 loc) • 6.5 kB
JavaScript
Object.defineProperty(exports,"__esModule",{value:true});exports.ImplType=undefined;var _slicedToArray=function(){function sliceIterator(arr,i){var _arr=[];var _n=true;var _d=false;var _e=undefined;try{for(var _i=arr[typeof Symbol==='function'?Symbol.iterator:'@@iterator'](),_s;!(_n=(_s=_i.next()).done);_n=true){_arr.push(_s.value);if(i&&_arr.length===i)break;}}catch(err){_d=true;_e=err;}finally{try{if(!_n&&_i["return"])_i["return"]();}finally{if(_d)throw _e;}}return _arr;}return function(arr,i){if(Array.isArray(arr)){return arr;}else if((typeof Symbol==='function'?Symbol.iterator:'@@iterator')in Object(arr)){return sliceIterator(arr,i);}else{throw new TypeError("Invalid attempt to destructure non-iterable instance");}};}();var _createClass=function(){function defineProperties(target,props){for(var i=0;i<props.length;i++){var descriptor=props[i];descriptor.enumerable=descriptor.enumerable||false;descriptor.configurable=true;if("value"in descriptor)descriptor.writable=true;Object.defineProperty(target,descriptor.key,descriptor);}}return function(Constructor,protoProps,staticProps){if(protoProps)defineProperties(Constructor.prototype,protoProps);if(staticProps)defineProperties(Constructor,staticProps);return Constructor;};}();var _reactNative=require('react-native');var _events=require('events');function _classCallCheck(instance,Constructor){if(!(instance instanceof Constructor)){throw new TypeError("Cannot call a class as a function");}}function _possibleConstructorReturn(self,call){if(!self){throw new ReferenceError("this hasn't been initialised - super() hasn't been called");}return call&&(typeof call==="object"||typeof call==="function")?call:self;}function _inherits(subClass,superClass){if(typeof superClass!=="function"&&superClass!==null){throw new TypeError("Super expression must either be null or a function, not "+typeof superClass);}subClass.prototype=Object.create(superClass&&superClass.prototype,{constructor:{value:subClass,enumerable:false,writable:true,configurable:true}});if(superClass)Object.setPrototypeOf?Object.setPrototypeOf(subClass,superClass):subClass.__proto__=superClass;}var RNZeroconf=_reactNative.NativeModules.RNZeroconf;var ImplType=exports.ImplType={NSD:'NSD',DNSSD:'DNSSD'};var Zeroconf=function(_EventEmitter){_inherits(Zeroconf,_EventEmitter);function Zeroconf(props){_classCallCheck(this,Zeroconf);var _this=_possibleConstructorReturn(this,(Zeroconf.__proto__||Object.getPrototypeOf(Zeroconf)).call(this,props));_this._services={};_this._publishedServices={};_this._dListeners={};_this.addDeviceListeners();return _this;}_createClass(Zeroconf,[{key:'addDeviceListeners',value:function addDeviceListeners(){var _this2=this;if(Object.keys(this._dListeners).length){return this.emit('error',new Error('RNZeroconf listeners already in place.'));}this._dListeners.start=_reactNative.DeviceEventEmitter.addListener('RNZeroconfStart',function(){return _this2.emit('start');});this._dListeners.stop=_reactNative.DeviceEventEmitter.addListener('RNZeroconfStop',function(){return _this2.emit('stop');});this._dListeners.error=_reactNative.DeviceEventEmitter.addListener('RNZeroconfError',function(err){if(_this2.listenerCount('error')>0){_this2.emit('error',new Error(err));}});this._dListeners.found=_reactNative.DeviceEventEmitter.addListener('RNZeroconfFound',function(service){if(!service||!service.name){return;}var name=service.name;_this2._services[name]=service;_this2.emit('found',name);_this2.emit('update');});this._dListeners.remove=_reactNative.DeviceEventEmitter.addListener('RNZeroconfRemove',function(service){if(!service||!service.name){return;}var name=service.name;delete _this2._services[name];_this2.emit('remove',name);_this2.emit('update');});this._dListeners.resolved=_reactNative.DeviceEventEmitter.addListener('RNZeroconfResolved',function(service){if(!service||!service.name){return;}_this2._services[service.name]=service;_this2.emit('resolved',service);_this2.emit('update');});this._dListeners.published=_reactNative.DeviceEventEmitter.addListener('RNZeroconfServiceRegistered',function(service){if(!service||!service.name){return;}_this2._publishedServices[service.name]=service;_this2.emit('published',service);});this._dListeners.unpublished=_reactNative.DeviceEventEmitter.addListener('RNZeroconfServiceUnregistered',function(service){if(!service||!service.name){return;}delete _this2._publishedServices[service.name];_this2.emit('unpublished',service);});}},{key:'removeDeviceListeners',value:function removeDeviceListeners(){var _this3=this;Object.keys(this._dListeners).forEach(function(name){return _this3._dListeners[name].remove();});this._dListeners={};}},{key:'getServices',value:function getServices(){return this._services;}},{key:'scan',value:function scan(){var type=arguments.length>0&&arguments[0]!==undefined?arguments[0]:'http';var protocol=arguments.length>1&&arguments[1]!==undefined?arguments[1]:'tcp';var domain=arguments.length>2&&arguments[2]!==undefined?arguments[2]:'local.';var implType=arguments.length>3&&arguments[3]!==undefined?arguments[3]:ImplType.NSD;this._services={};this.emit('update');if(_reactNative.Platform.OS==='android'){RNZeroconf.scan(type,protocol,domain,implType);}else{RNZeroconf.scan(type,protocol,domain);}}},{key:'stop',value:function stop(){var implType=arguments.length>0&&arguments[0]!==undefined?arguments[0]:ImplType.NSD;if(_reactNative.Platform.OS==='android'){RNZeroconf.stop(implType);}else{RNZeroconf.stop();}}},{key:'publishService',value:function publishService(type,protocol){var domain=arguments.length>2&&arguments[2]!==undefined?arguments[2]:'local.';var name=arguments[3];var port=arguments[4];var txt=arguments.length>5&&arguments[5]!==undefined?arguments[5]:{};var implType=arguments.length>6&&arguments[6]!==undefined?arguments[6]:ImplType.NSD;if(Object.keys(txt).length!==0){Object.entries(txt).map(function(_ref){var _ref2=_slicedToArray(_ref,2),key=_ref2[0],value=_ref2[1];return txt[key]=value.toString();});}if(_reactNative.Platform.OS==='android'){RNZeroconf.registerService(type,protocol,domain,name,port,txt,implType);}else{RNZeroconf.registerService(type,protocol,domain,name,port,txt);}}},{key:'unpublishService',value:function unpublishService(name){var implType=arguments.length>1&&arguments[1]!==undefined?arguments[1]:ImplType.NSD;if(_reactNative.Platform.OS==='android'){RNZeroconf.unregisterService(name,implType);}else{RNZeroconf.unregisterService(name);}}}]);return Zeroconf;}(_events.EventEmitter);exports.default=Zeroconf;