-
Notifications
You must be signed in to change notification settings - Fork 0
/
connect.js
38 lines (26 loc) · 902 Bytes
/
connect.js
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
var EventEmitter = require('events').EventEmitter
, net = require('net')
, mdns = require('mdns')
, split = require('split')
, connect = function () {
var browser = mdns.createBrowser(mdns.tcp('tomatotomato'))
, emitter = new EventEmitter()
browser.on('serviceUp', function (service) {
var connection = net.connect(service.port, service.host)
connection.pipe(split()).on('data', function (chunk) {
if (!chunk) return;
var data = JSON.parse(chunk)
data.port = service.port
data.host = service.host
emitter.emit('data', data)
})
// swallow the error, don't care
connection.on('error', function (err) {})
connection.on('close', function () {
emitter.emit('close')
})
})
browser.start()
return emitter
}
module.exports = connect