spacebro-client
Advanced tools
Comparing version 0.0.9 to 0.0.10
'use strict'; | ||
var mdns = require('../lib/mdns.js'); | ||
var io = require('socket.io-client'); | ||
@@ -8,58 +7,71 @@ var _ = require('lodash'); | ||
function registerToMaster(actionList, clientName, zeroconfName) { | ||
console.log('Wating for spacebro...'); | ||
mdns.connectToService(zeroconfName || 'spacebro', function socketioInit(err, address, port) { | ||
if (err) { | ||
console.log(err.stack); | ||
} | ||
console.log('---------------------------'); | ||
console.log('service found at address: ', address); | ||
socket = io('http://' + address + ':' + port).on('connect', function () { | ||
console.log('socketio connected to ' + 'http://' + address + ':' + port); | ||
var nameList = _.map(actionList, function (el) { | ||
return el.name; | ||
}); | ||
socket.emit('register', { eventsList: nameList, clientName: clientName || 'pid-' + process.pid }); | ||
var socketioInit = function socketioInit(err, address, port, actionList, clientName) { | ||
if (err) { | ||
console.log(err.stack); | ||
} | ||
console.log('---------------------------'); | ||
console.log('service found at address: ', address); | ||
socket = io('http://' + address + ':' + port).on('connect', function () { | ||
console.log('socketio connected to ' + 'http://' + address + ':' + port); | ||
var nameList = _.map(actionList, function (el) { | ||
return el.name; | ||
}); | ||
console.log('List of actions registered:'); | ||
var _iteratorNormalCompletion = true; | ||
var _didIteratorError = false; | ||
var _iteratorError = undefined; | ||
socket.emit('register', { eventsList: nameList, clientName: clientName || 'pid-' + process.pid }); | ||
}); | ||
console.log('List of actions registered:'); | ||
var _iteratorNormalCompletion = true; | ||
var _didIteratorError = false; | ||
var _iteratorError = undefined; | ||
try { | ||
var _loop = function _loop() { | ||
var action = _step.value; | ||
try { | ||
var _loop = function _loop() { | ||
var action = _step.value; | ||
console.log(action.name); | ||
socket.on(action.name, function (data) { | ||
if (action.trigger) { | ||
action.trigger(data); | ||
} | ||
}); | ||
}; | ||
console.log(action.name); | ||
socket.on(action.name, function (data) { | ||
if (action.trigger) { | ||
action.trigger(data); | ||
} | ||
}); | ||
}; | ||
for (var _iterator = actionList[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) { | ||
_loop(); | ||
for (var _iterator = actionList[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) { | ||
_loop(); | ||
} | ||
} catch (err) { | ||
_didIteratorError = true; | ||
_iteratorError = err; | ||
} finally { | ||
try { | ||
if (!_iteratorNormalCompletion && _iterator.return) { | ||
_iterator.return(); | ||
} | ||
} catch (err) { | ||
_didIteratorError = true; | ||
_iteratorError = err; | ||
} finally { | ||
try { | ||
if (!_iteratorNormalCompletion && _iterator.return) { | ||
_iterator.return(); | ||
} | ||
} finally { | ||
if (_didIteratorError) { | ||
throw _iteratorError; | ||
} | ||
if (_didIteratorError) { | ||
throw _iteratorError; | ||
} | ||
} | ||
} | ||
console.log('---------------------------'); | ||
console.log('---------------------------'); | ||
}; | ||
var registerToMaster = function registerToMaster(actionList, clientName, zeroconfName) { | ||
var mdns = require('../lib/mdns.js'); | ||
mdns.on('service-down', function (data) { | ||
console.log(data + ' is down =('); | ||
}); | ||
} | ||
console.log('Wating for spacebro...'); | ||
mdns.connectToService(zeroconfName || 'spacebro', function (err, address, port) { | ||
socketioInit(err, address, port, actionList, clientName); | ||
}); | ||
}; | ||
var socketioConnect = function socketioConnect(address, port, actionList, clientName) { | ||
socketioInit(null, address, port, actionList, clientName); | ||
}; | ||
module.exports = { | ||
registerToMaster: registerToMaster, | ||
socketioConnect: socketioConnect, | ||
emit: function emit(event, data) { | ||
@@ -66,0 +78,0 @@ if (socket) { |
@@ -6,39 +6,43 @@ 'use strict' | ||
module.exports = { | ||
connectToService: function (servicename, callback) { | ||
var services = [] | ||
var sequence = [ | ||
mdns.rst.DNSServiceResolve(), | ||
'DNSServiceGetAddrInfo' in mdns.dns_sd ? mdns.rst.DNSServiceGetAddrInfo() : mdns.rst.getaddrinfo({ | ||
families: [4] | ||
}), | ||
mdns.rst.makeAddressesUnique() | ||
] | ||
var browser = mdns.createBrowser(mdns.tcp(servicename), { | ||
resolverSequence: sequence | ||
}) | ||
browser.on('serviceUp', function (service) { | ||
// console.log('service up: ', service.type.name) | ||
if (_.findWhere(services, { | ||
var EventEmitter = require('events').EventEmitter | ||
var mdns_register = new EventEmitter() | ||
mdns_register.connectToService = function (servicename, callback) { | ||
var services = [] | ||
var sequence = [ | ||
mdns.rst.DNSServiceResolve(), | ||
'DNSServiceGetAddrInfo' in mdns.dns_sd ? mdns.rst.DNSServiceGetAddrInfo() : mdns.rst.getaddrinfo({ | ||
families: [4] | ||
}), | ||
mdns.rst.makeAddressesUnique() | ||
] | ||
var browser = mdns.createBrowser(mdns.tcp(servicename), { | ||
resolverSequence: sequence | ||
}) | ||
browser.on('serviceUp', function (service) { | ||
// console.log('service up: ', service.type.name) | ||
if (_.findWhere(services, { | ||
name: service.type.name, | ||
host: service.host.substr(0, service.host.length - 1), | ||
port: service.port | ||
}) === undefined) { | ||
// console.log('New socket.io connection with service "' + service.type.name + '" on: ' + service.host.substr(0, service.host.length - 1) + ':' + service.port) | ||
services.push({ | ||
name: service.type.name, | ||
host: service.host.substr(0, service.host.length - 1), | ||
port: service.port | ||
}) === undefined) { | ||
// console.log('New socket.io connection with service "' + service.type.name + '" on: ' + service.host.substr(0, service.host.length - 1) + ':' + service.port) | ||
services.push({ | ||
name: service.type.name, | ||
host: service.host.substr(0, service.host.length - 1), | ||
port: service.port | ||
}) | ||
callback(null, service.host.substr(0, service.host.length - 1), service.port) | ||
} | ||
}) | ||
browser.on('serviceDown', function (service) { | ||
console.log('service down: ', service.type.name) | ||
}) | ||
browser.on('error', function (Exp) { | ||
console.log(Exp) | ||
}) | ||
browser.start() | ||
} | ||
}) | ||
callback(null, service.host.substr(0, service.host.length - 1), service.port) | ||
} | ||
}) | ||
browser.on('serviceDown', function (service) { | ||
console.log('service down: ', service.type.name) | ||
mdns_register.emit('service-down', service.type.name) | ||
}) | ||
browser.on('error', function (Exp) { | ||
console.log(Exp) | ||
}) | ||
browser.start() | ||
} | ||
module.exports = mdns_register |
{ | ||
"name": "spacebro-client", | ||
"version": "0.0.9", | ||
"version": "0.0.10", | ||
"description": "Allow to automagically 🌟 connect to a Spacebro server", | ||
@@ -9,2 +9,3 @@ "main": "build/index.js", | ||
"example": "node ./example/index-example.js", | ||
"example-fallback": "node ./example/index-fallback-io", | ||
"build": "babel src -d build", | ||
@@ -11,0 +12,0 @@ "start": "node ./example/index-example.js" |
@@ -14,3 +14,6 @@ # Spacebro client | ||
spacebroClient = require('spacebro-client') | ||
// This means you will use mdns | ||
spacebroClient.registerToMaster(actionList, clientName, zeroconfName) | ||
// This means you know where to connect | ||
spacebroClient.socketioConnect(server, port, actionList, clientName, zeroconfName) | ||
spacebroClient.emit('event', data) | ||
@@ -17,0 +20,0 @@ ``` |
'use strict' | ||
const mdns = require('../lib/mdns.js') | ||
const io = require('socket.io-client') | ||
@@ -8,33 +8,47 @@ const _ = require('lodash') | ||
function registerToMaster (actionList, clientName, zeroconfName) { | ||
console.log('Wating for spacebro...') | ||
mdns.connectToService(zeroconfName || 'spacebro', function socketioInit (err, address, port) { | ||
if (err) { | ||
console.log(err.stack) | ||
} | ||
console.log('---------------------------') | ||
console.log('service found at address: ', address) | ||
socket = io('http://' + address + ':' + port) | ||
.on('connect', function () { | ||
console.log('socketio connected to ' + 'http://' + address + ':' + port) | ||
var nameList = _.map(actionList, function (el) { | ||
return el.name | ||
}) | ||
socket.emit('register', { eventsList: nameList, clientName: clientName || 'pid-' + process.pid }) | ||
var socketioInit = function (err, address, port, actionList, clientName) { | ||
if (err) { | ||
console.log(err.stack) | ||
} | ||
console.log('---------------------------') | ||
console.log('service found at address: ', address) | ||
socket = io('http://' + address + ':' + port) | ||
.on('connect', function () { | ||
console.log('socketio connected to ' + 'http://' + address + ':' + port) | ||
var nameList = _.map(actionList, function (el) { | ||
return el.name | ||
}) | ||
console.log('List of actions registered:') | ||
for (let action of actionList) { | ||
console.log(action.name) | ||
socket.on(action.name, function (data) { | ||
if (action.trigger) { | ||
action.trigger(data) | ||
} | ||
}) | ||
} | ||
console.log('---------------------------') | ||
socket.emit('register', { eventsList: nameList, clientName: clientName || 'pid-' + process.pid }) | ||
}) | ||
console.log('List of actions registered:') | ||
for (let action of actionList) { | ||
console.log(action.name) | ||
socket.on(action.name, function (data) { | ||
if (action.trigger) { | ||
action.trigger(data) | ||
} | ||
}) | ||
} | ||
console.log('---------------------------') | ||
} | ||
var registerToMaster = function (actionList, clientName, zeroconfName) { | ||
let mdns = require('../lib/mdns.js') | ||
mdns.on('service-down', function (data) { | ||
console.log(data + ' is down =(') | ||
}) | ||
console.log('Wating for spacebro...') | ||
mdns.connectToService(zeroconfName || 'spacebro', function (err, address, port) { | ||
socketioInit(err, address, port, actionList, clientName) | ||
}) | ||
} | ||
var socketioConnect = function (address, port, actionList, clientName) { | ||
socketioInit(null, address, port, actionList, clientName) | ||
} | ||
module.exports = { | ||
registerToMaster: registerToMaster, | ||
socketioConnect: socketioConnect, | ||
emit: function (event, data) { | ||
@@ -41,0 +55,0 @@ if (socket) { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
10936
8
207
52