-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathServiceDiscovery.js
More file actions
70 lines (55 loc) · 1.57 KB
/
ServiceDiscovery.js
File metadata and controls
70 lines (55 loc) · 1.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
'use strict';
var util = require('util');
var EventEmitter = require('events').EventEmitter;
var {
NativeEventEmitter,
NativeModules
} = require('react-native');
var Native = NativeModules.ServiceDiscovery;
function ServiceDiscovery() {
this._eventEmitter = new NativeEventEmitter(Native);
if (EventEmitter instanceof Function) {
EventEmitter.call(this);
}
if (!this.on) {
this.on = this.addListener.bind(this);
}
}
util.inherits(ServiceDiscovery, EventEmitter);
ServiceDiscovery.prototype.start = function(udpPort) {
this._registerEvents();
Native.start(udpPort);
};
ServiceDiscovery.prototype.stop = function() {
Native.stop();
};
ServiceDiscovery.prototype._registerEvents = function() {
if (this._subs && this._subs.length > 0) {
return;
}
this._subs = [
this._eventEmitter.addListener('service-resolved', ev => {
if (this._id !== ev.id) {
return;
}
this._onServiceResolved(ev);
}),
this._eventEmitter.addListener('udp-discovery', ev => {
if (this._id !== ev.id) {
return;
}
this._onUdpDiscovery(ev);
}),
];
};
ServiceDiscovery.prototype._onServiceResolved = function(ev) {
this.emit('service-resolved', ev);
};
ServiceDiscovery.prototype._onUdpDiscovery = function(ev) {
this.emit('udp-discovery', ev);
};
ServiceDiscovery.prototype._unregisterEvents = function() {
this._subs.forEach(e => e.remove());
this._subs = [];
};
module.exports = ServiceDiscovery;