Skip to content

Commit

Permalink
Added logic to unsubscribe methods on connection "disconnect"
Browse files Browse the repository at this point in the history
- 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
NTaylorMullen committed Nov 6, 2012
1 parent b877760 commit a20ab6a
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand All @@ -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];
Expand All @@ -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));
}
}
}
Expand All @@ -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');
Expand Down
27 changes: 20 additions & 7 deletions src/Microsoft.AspNet.SignalR.Core/Scripts/hubs.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand All @@ -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];
Expand All @@ -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));
}
}
}
Expand All @@ -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*/
Expand Down

0 comments on commit a20ab6a

Please sign in to comment.