From 1440ca2b3c7b05e9aad784791281fad66b0b43ba Mon Sep 17 00:00:00 2001 From: Alexey Raksha Date: Wed, 22 Jul 2015 11:35:06 +0300 Subject: [PATCH 1/3] method open new connection --- src/angular-wamp.js | 40 ++++++++++++++++++++++++++++------------ 1 file changed, 28 insertions(+), 12 deletions(-) diff --git a/src/angular-wamp.js b/src/angular-wamp.js index ac32ad1..013e2d6 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 @@ -364,6 +366,20 @@ if (typeof module !== "undefined" && typeof exports !== "undefined" && module.ex return { connection: connection, + openNewConnection : function(newOptions, open) { + //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); + 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) { From 8a981beba1a31ee1589373ca62d5ce9b6bc9209c Mon Sep 17 00:00:00 2001 From: Alexey Raksha Date: Wed, 22 Jul 2015 16:07:33 +0300 Subject: [PATCH 2/3] method open new connection. Stop old connection broadcast --- src/angular-wamp.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/angular-wamp.js b/src/angular-wamp.js index 013e2d6..aad6a26 100644 --- a/src/angular-wamp.js +++ b/src/angular-wamp.js @@ -366,7 +366,7 @@ if (typeof module !== "undefined" && typeof exports !== "undefined" && module.ex return { connection: connection, - openNewConnection : function(newOptions, open) { + openNewConnection : function(newOptions, open, stopOldConnectionBroadcast) { //close old connection if (connection.isOpen) { connection.close(); @@ -374,6 +374,10 @@ if (typeof module !== "undefined" && typeof exports !== "undefined" && module.ex } 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) { From 346a59082876fa07db67e1ce26890d18b3f1e690 Mon Sep 17 00:00:00 2001 From: Alexey Raksha Date: Tue, 4 Aug 2015 14:18:21 +0300 Subject: [PATCH 3/3] $wamp.connection always return initial connection. Connection can be changed. --- README.md | 2 +- src/angular-wamp.js | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-) 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 aad6a26..c473735 100644 --- a/src/angular-wamp.js +++ b/src/angular-wamp.js @@ -365,7 +365,9 @@ 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) {