Skip to content

Commit

Permalink
v2.2.8 Add SRV failover
Browse files Browse the repository at this point in the history
  • Loading branch information
Roy van de Water committed Mar 2, 2017
1 parent f2006e9 commit cdb6e34
Show file tree
Hide file tree
Showing 4 changed files with 2,551 additions and 11 deletions.
7 changes: 3 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ Version 2.0.0 of this library introduces some major API changes that make it inc
* [meshblu.unregister(query, callback)](#meshbluunregisterquery-callback)
* [meshblu.unsubscribe(params)](#meshbluunsubscribeparams)
* [meshblu.update(query, callback)](#meshbluupdatequeryupdate-callback)
* [meshblu.whoami(obj, callback)](#meshbluwhoamiobj-callback)
* [meshblu.whoami(callback)](#meshbluwhoamicallback)

# Getting Started

Expand Down Expand Up @@ -716,13 +716,12 @@ meshblu.update({uuid: 'i-made-this-uuid-up', color: 'blue'}, function(result){
});
```

## meshblu.whoami(obj, callback)
## meshblu.whoami(callback)

Retrieve the device the connection is currently authenticated as from the Meshblu device registery.

##### Arguments

* `obj` Object. Anything may be put here, but it will be ignored. Is preserved for backwards compatibility.
* `callback` Function that will be called with a `device`.
* `device` Full device from the Meshblu device registry.

Expand All @@ -732,7 +731,7 @@ Calling whoami:
When whoami is called:

```javascript
meshblu.whoami({doesnt: 'matter', one: 'bit'}, function(device){
meshblu.whoami(function(device){
console.log('whoami');
console.log(JSON.stringify(device, null, 2));
// whoami
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "meshblu",
"description": "Meshblu IoT network and API",
"version": "2.2.7",
"version": "2.2.8",
"license": "MIT",
"homepage": "https://developer.octoblu.com",
"repository": "git://github.com/octoblu/meshblu-npm",
Expand Down Expand Up @@ -31,6 +31,7 @@
"node-rsa": "^0.2.23",
"request": "^2.58.0",
"socket.io-client": "^1.3.5",
"srv-failover": "^1.3.0",
"tar": "^2.1.1",
"url": "^0.10.3"
},
Expand Down
34 changes: 28 additions & 6 deletions src/srv-socket.coffee
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
Backoff = require 'backo'
dns = require 'dns'
_ = require 'lodash'
socketIoClient = require 'socket.io-client'
SrvFailover = require 'srv-failover'
url = require 'url'
ProxySocket = require './proxy-socket'

Expand All @@ -9,18 +11,36 @@ class SrvSocket extends ProxySocket
@_socketIoClient = dependencies.socketIoClient ? socketIoClient
@_dns = dependencies.dns ? dns
@_options = {protocol, hostname, port, service, domain, secure, resolveSrv}
@_socketIoOptions = _.defaults {}, socketIoOptions, {forceNew: true}
@_socketIoOptions = _.defaults {}, socketIoOptions, {forceNew: true, reconnect: false}

@backoff = new Backoff

return unless resolveSrv?
srvProtocol = 'socket-io-wss'
urlProtocol = 'wss'

if secure == false
srvProtocol = 'socket-io-ws'
urlProtocol = 'ws'

@_srvFailover = new SrvFailover {domain, service, protocol: srvProtocol, urlProtocol}

close: (callback) =>
return callback() unless @_socket?
@_socket.once 'disconnect', => callback()
@_socket.close()

connect: (callback) =>
callback = _.once callback

@_resolveUri (error, uri) =>
return callback error if error?
@_socket = @_socketIoClient(uri, @_socketIoOptions)
@_socket.once 'connect', => callback()
@_socket.once 'connect_error', (error) =>
return callback error unless @_srvFailover?
@_srvFailover.markBadUrl uri, ttl: 60000
_.delay @connect, @backoff.duration(), callback

@_proxyDefaultIncomingEvents() # From super

Expand All @@ -41,13 +61,15 @@ class SrvSocket extends ProxySocket
return 'socket-io-ws'

_resolveUri: (callback) =>
{protocol, hostname, port, resolveSrv} = @_options
return callback null, url.format({protocol, hostname, port, slashes: true}) unless resolveSrv
{protocol, hostname, port} = @_options
return callback null, url.format({protocol, hostname, port, slashes: true}) unless @_srvFailover?

@_dns.resolveSrv @_getSrvAddress(), (error, addresses) =>
return @_srvFailover.resolveUrl (error, baseUrl) =>
if error && error.noValidAddresses
@_srvFailover.clearBadUrls()
return @_resolveUri callback
return callback error if error?
return callback new Error('SRV record found, but contained no valid addresses') if _.isEmpty addresses
return callback null, @_resolveUrlFromAddresses(addresses)
return callback null, baseUrl

_resolveUrlFromAddresses: (addresses) =>
address = _.minBy addresses, 'priority'
Expand Down
Loading

0 comments on commit cdb6e34

Please sign in to comment.