spacebro-client
Advanced tools
Comparing version
@@ -5,3 +5,5 @@ 'use strict'; | ||
var _ = require('lodash'); | ||
var socket = undefined; | ||
var staticPort = undefined, | ||
staticAddress = undefined; | ||
var sockets = []; | ||
@@ -12,5 +14,7 @@ var socketioInit = function socketioInit(err, address, port, actionList, clientName) { | ||
} | ||
var socket = undefined; | ||
console.log('---------------------------'); | ||
console.log('service found at address: ', address); | ||
socket = io('http://' + address + ':' + port).on('connect', function () { | ||
socket = io('http://' + address + ':' + port); | ||
socket.on('connect', function () { | ||
sockets.push(socket); | ||
console.log('socketio connected to ' + 'http://' + address + ':' + port); | ||
@@ -21,54 +25,66 @@ var nameList = _.map(actionList, function (el) { | ||
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; | ||
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(); | ||
} | ||
} catch (err) { | ||
_didIteratorError = true; | ||
_iteratorError = err; | ||
} finally { | ||
try { | ||
if (!_iteratorNormalCompletion && _iterator.return) { | ||
_iterator.return(); | ||
for (var _iterator = actionList[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) { | ||
_loop(); | ||
} | ||
} catch (err) { | ||
_didIteratorError = true; | ||
_iteratorError = err; | ||
} finally { | ||
if (_didIteratorError) { | ||
throw _iteratorError; | ||
try { | ||
if (!_iteratorNormalCompletion && _iterator.return) { | ||
_iterator.return(); | ||
} | ||
} finally { | ||
if (_didIteratorError) { | ||
throw _iteratorError; | ||
} | ||
} | ||
} | ||
} | ||
console.log('---------------------------'); | ||
console.log('---------------------------'); | ||
}); | ||
socket.on('disconnect', function () { | ||
console.log('socket down: ', address + ':' + port); | ||
sockets.splice(sockets.indexOf(socket), 1); | ||
}); | ||
}; | ||
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); | ||
}); | ||
if (staticAddress) { | ||
socketioInit(null, staticAddress, staticPort, actionList, clientName); | ||
} else { | ||
var mdns = require('../lib/mdns.js'); | ||
// not useful as it is already in lib/mdns.js | ||
/* | ||
mdns.on('service-down', function (data) { | ||
console.log(data + ' is down =(') | ||
}) | ||
*/ | ||
console.log('Waiting 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); | ||
var iKnowMyMaster = function iKnowMyMaster(address, port) { | ||
staticPort = port; | ||
staticAddress = address; | ||
}; | ||
@@ -78,8 +94,8 @@ | ||
registerToMaster: registerToMaster, | ||
socketioConnect: socketioConnect, | ||
iKnowMyMaster: iKnowMyMaster, | ||
emit: function emit(event, data) { | ||
if (socket) { | ||
sockets.forEach(function (socket) { | ||
socket.emit(event, data); | ||
} | ||
}); | ||
} | ||
}; |
@@ -22,3 +22,3 @@ 'use strict' | ||
browser.on('serviceUp', function (service) { | ||
// console.log('service up: ', service.type.name) | ||
console.log('service up: ', service.type.name) | ||
if (_.findWhere(services, { | ||
@@ -25,0 +25,0 @@ name: service.type.name, |
{ | ||
"name": "spacebro-client", | ||
"version": "0.0.10", | ||
"version": "0.0.11", | ||
"description": "Allow to automagically 🌟 connect to a Spacebro server", | ||
@@ -9,3 +9,3 @@ "main": "build/index.js", | ||
"example": "node ./example/index-example.js", | ||
"example-fallback": "node ./example/index-fallback-io", | ||
"example-no-mdns": "node ./example/index-example-no-mdns.js", | ||
"build": "babel src -d build", | ||
@@ -12,0 +12,0 @@ "start": "node ./example/index-example.js" |
'use strict' | ||
const io = require('socket.io-client') | ||
const _ = require('lodash') | ||
let socket | ||
let staticPort, staticAddress | ||
let sockets = [] | ||
var socketioInit = function (err, address, port, actionList, clientName) { | ||
@@ -13,47 +12,61 @@ if (err) { | ||
} | ||
let socket | ||
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.on('connect', function () { | ||
sockets.push(socket) | ||
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 }) | ||
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) | ||
} | ||
}) | ||
socket.emit('register', { eventsList: nameList, clientName: clientName || 'pid-' + process.pid }) | ||
} | ||
console.log('---------------------------') | ||
}) | ||
socket.on('disconnect', function () { | ||
console.log('socket down: ', address + ':' + port) | ||
sockets.splice(sockets.indexOf(socket), 1) | ||
}) | ||
} | ||
var registerToMaster = function (actionList, clientName, zeroconfName) { | ||
if (staticAddress) { | ||
socketioInit(null, staticAddress, staticPort, actionList, clientName) | ||
} else { | ||
let mdns = require('../lib/mdns.js') | ||
// event propagation for andling error at the top level | ||
// next is implement EventEmitter in spacebro | ||
/* | ||
mdns.on('service-down', function (data) { | ||
console.log(data + ' is down =(') | ||
}) | ||
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('Waiting for spacebro...') | ||
mdns.connectToService(zeroconfName || 'spacebro', function (err, address, port) { | ||
socketioInit(err, address, port, actionList, clientName) | ||
}) | ||
} | ||
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 iKnowMyMaster = function (address, port) { | ||
staticPort = port | ||
staticAddress = address | ||
} | ||
var socketioConnect = function (address, port, actionList, clientName) { | ||
socketioInit(null, address, port, actionList, clientName) | ||
} | ||
module.exports = { | ||
registerToMaster: registerToMaster, | ||
socketioConnect: socketioConnect, | ||
iKnowMyMaster: iKnowMyMaster, | ||
emit: function (event, data) { | ||
if (socket) { | ||
sockets.forEach(function (socket) { | ||
socket.emit(event, data) | ||
} | ||
}) | ||
} | ||
} |
11825
8.13%239
15.46%