Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added logic to unsubscribe methods on connection "disconnect"

- What was happening is that once a connection was stopped and then
re-started it was re-registering all of the .client hub methods.  This
only occured when using /signalr/hubs because we try to handle all of
the subscriptions for devs.
  • Loading branch information...
commit a20ab6a98a5f8197a5701adafd5b413668ab5f48 1 parent b877760
@NTaylorMullen NTaylorMullen authored
View
27 samples/Microsoft.AspNet.SignalR.Hosting.AspNet.Samples/Scripts/hubs.js
@@ -27,8 +27,8 @@
};
}
- function createHubProxies(instance) {
- var key, hub, memberKey, memberValue;
+ function registerHubProxies(instance, shouldSubscribe) {
+ var key, hub, memberKey, memberValue, subscriptionMethod;
for (key in instance) {
if (instance.hasOwnProperty(key)) {
@@ -39,7 +39,16 @@
continue;
}
- // Loop through all members on the hub and find client hub functions to subscribe to
+ if (shouldSubscribe) {
+ // We want to subscribe to the hub events
+ subscriptionMethod = hub.on;
+ }
+ else {
+ // We want to unsubscribe from the hub events
+ subscriptionMethod = hub.off;
+ }
+
+ // Loop through all members on the hub and find client hub functions to subscribe/unsubscribe
for (memberKey in hub.client) {
if (hub.client.hasOwnProperty(memberKey)) {
memberValue = hub.client[memberKey];
@@ -49,8 +58,7 @@
continue;
}
- // Subscribe to the hub event for this method
- hub.on(memberKey, makeProxyCallback(hub, memberValue));
+ subscriptionMethod.call(hub, memberKey, makeProxyCallback(hub, memberValue));
}
}
}
@@ -59,10 +67,15 @@
signalR.hub = $.hubConnection("/signalr", { useDefaultPath: false })
.starting(function () {
- // Subscribe and create the hub proxies
- createHubProxies(signalR, this);
+ // Register the hub proxies as subscribed
+ // (instance, shouldSubscribe)
+ registerHubProxies(signalR, true);
this._registerSubscribedHubs();
+ }).disconnected(function () {
+ // Unsubscribe all hub proxies when we "disconnect". This is to ensure that we do not re-add functional call backs.
+ // (instance, shouldSubscribe)
+ registerHubProxies(signalR, false);
});
signalR.adminAuthHub = signalR.hub.createHubProxy('adminAuthHub');
View
27 src/Microsoft.AspNet.SignalR.Core/Scripts/hubs.js
@@ -27,8 +27,8 @@
};
}
- function createHubProxies(instance) {
- var key, hub, memberKey, memberValue;
+ function registerHubProxies(instance, shouldSubscribe) {
+ var key, hub, memberKey, memberValue, subscriptionMethod;
for (key in instance) {
if (instance.hasOwnProperty(key)) {
@@ -39,7 +39,16 @@
continue;
}
- // Loop through all members on the hub and find client hub functions to subscribe to
+ if (shouldSubscribe) {
+ // We want to subscribe to the hub events
+ subscriptionMethod = hub.on;
+ }
+ else {
+ // We want to unsubscribe from the hub events
+ subscriptionMethod = hub.off;
+ }
+
+ // Loop through all members on the hub and find client hub functions to subscribe/unsubscribe
for (memberKey in hub.client) {
if (hub.client.hasOwnProperty(memberKey)) {
memberValue = hub.client[memberKey];
@@ -49,8 +58,7 @@
continue;
}
- // Subscribe to the hub event for this method
- hub.on(memberKey, makeProxyCallback(hub, memberValue));
+ subscriptionMethod.call(hub, memberKey, makeProxyCallback(hub, memberValue));
}
}
}
@@ -59,10 +67,15 @@
signalR.hub = $.hubConnection("{serviceUrl}", { useDefaultPath: false })
.starting(function () {
- // Subscribe and create the hub proxies
- createHubProxies(signalR, this);
+ // Register the hub proxies as subscribed
+ // (instance, shouldSubscribe)
+ registerHubProxies(signalR, true);
this._registerSubscribedHubs();
+ }).disconnected(function () {
+ // Unsubscribe all hub proxies when we "disconnect". This is to ensure that we do not re-add functional call backs.
+ // (instance, shouldSubscribe)
+ registerHubProxies(signalR, false);
});
/*hubs*/
Please sign in to comment.
Something went wrong with that request. Please try again.