diff --git a/README.md b/README.md index 3404d2d..bd32aa7 100644 --- a/README.md +++ b/README.md @@ -174,7 +174,7 @@ You can also access the ``connection`` and ``session`` through the ``$wamp`` ser ```Javascript $wamp.session; -$wamp.connection; +$wamp.connection(); ``` ###Interceptors diff --git a/src/angular-wamp.js b/src/angular-wamp.js index ac32ad1..c473735 100644 --- a/src/angular-wamp.js +++ b/src/angular-wamp.js @@ -236,19 +236,21 @@ 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}); - sessionDeferred.resolve(); - }); - - connection.onclose = digestWrapper(function (reason, details) { - $log.debug("Connection Closed: ", reason, details); - sessionDeferred = $q.defer(); - sessionPromise = sessionDeferred.promise; - $rootScope.$broadcast("$wamp.close", {reason: reason, details: details}); - }); + function bindOpenClose() { + connection.onopen = digestWrapper(function (session, details) { + $log.debug("Congrats! You're connected to the WAMP server!"); + $rootScope.$broadcast("$wamp.open", {session: session, details: details}); + sessionDeferred.resolve(); + }); + connection.onclose = digestWrapper(function (reason, details) { + $log.debug("Connection Closed: ", reason, details); + sessionDeferred = $q.defer(); + sessionPromise = sessionDeferred.promise; + $rootScope.$broadcast("$wamp.close", {reason: reason, details: details}); + }); + } + bindOpenClose(); /** * Subscription object which self manages reconnections @@ -363,7 +365,27 @@ if (typeof module !== "undefined" && typeof exports !== "undefined" && module.ex }; return { - connection: connection, + connection: function(){ + return connection; + }, + openNewConnection : function(newOptions, open, stopOldConnectionBroadcast) { + //close old connection + if (connection.isOpen) { + connection.close(); + //oldConnection.onclose will emit + } + options = newOptions; + options = angular.extend({onchallenge: digestWrapper(onchallenge), use_deferred: $q.defer}, options); + if (stopOldConnectionBroadcast) { + connection.onclose = null; + connection.onopen = null; + } + connection = new autobahn.Connection(options); + bindOpenClose(); + if (open) { + connection.open(); + } + }, open: function () { //If using WAMP CRA we need to get the authid before the connection can be opened. if (options.authmethods && options.authmethods.indexOf('wampcra') !== -1 && !options.authid) {