Skip to content

Commit

Permalink
Added ability to have multiple connections
Browse files Browse the repository at this point in the history
  • Loading branch information
davidwdan committed Apr 30, 2016
1 parent 9c14e8f commit 3fd1f69
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 23 deletions.
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,28 @@ $wamp.session;
$wamp.connection;
```

### Multiple Connections

You can create multiple connections by creating a new provider that wraps `$wampProvider`. You'll also need to specify a `prefix` for that services wamp events, which will be used instead of `$wamp`.

```JS
app.provider('$wamp2', function ($wampProvider) {

var options = {
url: 'ws://127.0.0.1:9992',
realm: 'thruway.auth',
prefix: 'wamp2'
};

this.$get = function ($injector) {
$wampProvider.init(options);
return $injector.invoke($wampProvider.$get);
};
})

```
You can now use the service `$wamp2` exactly like `$wamp`.

###Interceptors

AngularWAMP supports [$http style interceptors](https://docs.angularjs.org/api/ng/service/$http#interceptors)
Expand Down
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "angular-wamp",
"version": "0.1.8",
"version": "0.2.0",
"main": "./release/angular-wamp.js",
"authors": [
"David Dan <davidwdan@gmail.com>"
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "angular-wamp",
"description": "Angular library for Autobahn.js (WAMP v2)",
"version": "0.1.8",
"version": "0.2.0",
"author": "David Dan <davidwdan@gmail.com>",
"homepage": "https://github.com/voryx/angular-wamp",
"repository": {
Expand Down
22 changes: 12 additions & 10 deletions release/angular-wamp.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* commonjs package manager support */
if (typeof module !== "undefined" && typeof exports !== "undefined" && module.exports === exports){
if (typeof module !== "undefined" && typeof exports !== "undefined" && module.exports === exports) {
var autobahn = require('autobahn');
module.exports = 'vxWamp';
}
Expand Down Expand Up @@ -58,6 +58,8 @@ if (typeof module !== "undefined" && typeof exports !== "undefined" && module.ex
*
* Optional options:
*
* - `prefix`: `{string=}` - prefix events so we can distinguish between instances (default: '$wamp')
*
* Options that control what kind of Deferreds to use:
*
* - `use_es6_promises`: `{boolean=}` - use deferreds based on ES6 promises
Expand Down Expand Up @@ -179,6 +181,9 @@ if (typeof module !== "undefined" && typeof exports !== "undefined" && module.ex
var connection;
var sessionDeferred = $q.defer();
var sessionPromise = sessionDeferred.promise;
var defaultOptions = {onchallenge: digestWrapper(onchallenge), use_deferred: $q.defer, prefix: '$wamp'};

options = angular.extend(defaultOptions, options);

/**
* @param session
Expand All @@ -193,7 +198,7 @@ if (typeof module !== "undefined" && typeof exports !== "undefined" && module.ex

var onChallengeDeferred = $q.defer();

$rootScope.$broadcast("$wamp.onchallenge", {
$rootScope.$broadcast(options.prefix + ".onchallenge", {
promise: onChallengeDeferred,
session: session,
method: method,
Expand All @@ -210,7 +215,7 @@ if (typeof module !== "undefined" && typeof exports !== "undefined" && module.ex

angular.forEach(interceptors, function (interceptor) {
reversedInterceptors.unshift(
angular.isString(interceptor) ? $injector.get(interceptor) : $injector.invoke(interceptor));
angular.isString(interceptor) ? $injector.get(interceptor) : $injector.invoke(interceptor));
});

/**
Expand All @@ -233,12 +238,10 @@ if (typeof module !== "undefined" && typeof exports !== "undefined" && module.ex
};
}

options = angular.extend({onchallenge: digestWrapper(onchallenge), use_deferred: $q.defer}, options);

connection = new autobahn.Connection(options);
connection.onopen = digestWrapper(function (session, details) {
$log.debug("Congrats! You're connected to the WAMP server!");
$rootScope.$broadcast("$wamp.open", {session: session, details: details});
$rootScope.$broadcast(options.prefix + ".open", {session: session, details: details});
sessionDeferred.resolve();
});

Expand All @@ -254,10 +257,9 @@ if (typeof module !== "undefined" && typeof exports !== "undefined" && module.ex
}
}

$rootScope.$broadcast("$wamp.close", {reason: reason, details: details});
$rootScope.$broadcast(options.prefix + ".close", {reason: reason, details: details});
});


/**
* Subscription object which self manages reconnections
* @param topic
Expand Down Expand Up @@ -295,7 +297,7 @@ if (typeof module !== "undefined" && typeof exports !== "undefined" && module.ex
onOpen();
}

unregister = $rootScope.$on("$wamp.open", onOpen);
unregister = $rootScope.$on(options.prefix + ".open", onOpen);

subscription.promise = deferred.promise;
subscription.unsubscribe = function () {
Expand Down Expand Up @@ -333,7 +335,7 @@ if (typeof module !== "undefined" && typeof exports !== "undefined" && module.ex
* @returns {{error: *, type: *, args: *}}
*/
var error = function (error) {
$log.error("$wamp error", {type: type, arguments: args, error: error});
$log.error(options.prefix + " error", {type: type, arguments: args, error: error});
return $q.reject({error: error, type: type, args: args});
};

Expand Down
2 changes: 1 addition & 1 deletion release/angular-wamp.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 12 additions & 10 deletions src/angular-wamp.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* commonjs package manager support */
if (typeof module !== "undefined" && typeof exports !== "undefined" && module.exports === exports){
if (typeof module !== "undefined" && typeof exports !== "undefined" && module.exports === exports) {
var autobahn = require('autobahn');
module.exports = 'vxWamp';
}
Expand Down Expand Up @@ -58,6 +58,8 @@ if (typeof module !== "undefined" && typeof exports !== "undefined" && module.ex
*
* Optional options:
*
* - `prefix`: `{string=}` - prefix events so we can distinguish between instances (default: '$wamp')
*
* Options that control what kind of Deferreds to use:
*
* - `use_es6_promises`: `{boolean=}` - use deferreds based on ES6 promises
Expand Down Expand Up @@ -179,6 +181,9 @@ if (typeof module !== "undefined" && typeof exports !== "undefined" && module.ex
var connection;
var sessionDeferred = $q.defer();
var sessionPromise = sessionDeferred.promise;
var defaultOptions = {onchallenge: digestWrapper(onchallenge), use_deferred: $q.defer, prefix: '$wamp'};

options = angular.extend(defaultOptions, options);

/**
* @param session
Expand All @@ -193,7 +198,7 @@ if (typeof module !== "undefined" && typeof exports !== "undefined" && module.ex

var onChallengeDeferred = $q.defer();

$rootScope.$broadcast("$wamp.onchallenge", {
$rootScope.$broadcast(options.prefix + ".onchallenge", {
promise: onChallengeDeferred,
session: session,
method: method,
Expand All @@ -210,7 +215,7 @@ if (typeof module !== "undefined" && typeof exports !== "undefined" && module.ex

angular.forEach(interceptors, function (interceptor) {
reversedInterceptors.unshift(
angular.isString(interceptor) ? $injector.get(interceptor) : $injector.invoke(interceptor));
angular.isString(interceptor) ? $injector.get(interceptor) : $injector.invoke(interceptor));
});

/**
Expand All @@ -233,12 +238,10 @@ if (typeof module !== "undefined" && typeof exports !== "undefined" && module.ex
};
}

options = angular.extend({onchallenge: digestWrapper(onchallenge), use_deferred: $q.defer}, options);

connection = new autobahn.Connection(options);
connection.onopen = digestWrapper(function (session, details) {
$log.debug("Congrats! You're connected to the WAMP server!");
$rootScope.$broadcast("$wamp.open", {session: session, details: details});
$rootScope.$broadcast(options.prefix + ".open", {session: session, details: details});
sessionDeferred.resolve();
});

Expand All @@ -254,10 +257,9 @@ if (typeof module !== "undefined" && typeof exports !== "undefined" && module.ex
}
}

$rootScope.$broadcast("$wamp.close", {reason: reason, details: details});
$rootScope.$broadcast(options.prefix + ".close", {reason: reason, details: details});
});


/**
* Subscription object which self manages reconnections
* @param topic
Expand Down Expand Up @@ -295,7 +297,7 @@ if (typeof module !== "undefined" && typeof exports !== "undefined" && module.ex
onOpen();
}

unregister = $rootScope.$on("$wamp.open", onOpen);
unregister = $rootScope.$on(options.prefix + ".open", onOpen);

subscription.promise = deferred.promise;
subscription.unsubscribe = function () {
Expand Down Expand Up @@ -333,7 +335,7 @@ if (typeof module !== "undefined" && typeof exports !== "undefined" && module.ex
* @returns {{error: *, type: *, args: *}}
*/
var error = function (error) {
$log.error("$wamp error", {type: type, arguments: args, error: error});
$log.error(options.prefix + " error", {type: type, arguments: args, error: error});
return $q.reject({error: error, type: type, args: args});
};

Expand Down

0 comments on commit 3fd1f69

Please sign in to comment.