I am learning how to use AngularJS and SignalR together and I am wondering if I can restart a SignalR connection withot losing the connectionId on the server side. The reason I am asking this has got to do with the client methods that needed to be called serverside. I haven't tried anything out yet and I just was thinking about this situation and what is the best practice solution and I am hoping some could think along or might have the solution and would like to explain it.
For example: I have two angularJS controllers, controller1
and controller2
and two signalR hubs, hub1
and hub2
. controller1
is started on opening the website and in the initialisation of controller1
I can bind a function to a client method that needs to be called in hub1
before SignalR is started. This works fine and even after signalR is started I can still bind functions to client methods with the on
function even if the signalR is started, although this probably isn't nessecary because I can bind the functions to the client methods before starting the signalR connection.
Next, on a form I got a button and that button is starting another div which has controller2
as ng-controller
. In the initialisation of controller2
I want to bind functions to client methods that needs to be called in hub2
. But since the signalR connection is already started by controller1
, I can't do hub2.client.AMethod = function () { }
. I was thinking, would this be possible if I can restart a signalR connection without losing the connectionId on the server side and by doing the restart, also refresh all the client methods bindings? And if not, can I use the on
function even if there hasn't been a function binded to client method on hub2
before? Or do I have to bind an empty function to a client method on hub2
as well before I start my signalR connection?
EDIT: I took the time to set up a code example.
I got the 2 hubs: Hub1
[HubName("Hub1")]
public class Hub1 : Hub
{
public void TestMethod1(string test)
{
Clients.All.TestMethod1Hub1("Testing Hub1 method1; " + test);
}
public void TestMethod2(string test)
{
Clients.All.TestMethod2Hub1("Testing Hub1 method2; " + test);
}
}
and hub2:
[HubName("Hub2")]
public class Hub2 : Hub
{
public void TestMethod1(string test)
{
Clients.All.TestMethod1Hub2("Testing Hub2 method1; " + test);
}
public void TestMethod2(string test)
{
Clients.All.TestMethod2Hub2("Testing Hub2 method2; " + test);
}
}
And I got my angularJS controller:
testApp.controller('controller1', ['$scope', 'signalRService', function ($scope, signalRService) {
var self = this;
$scope.logs = [];
self.TestMethod = function(testString) {
$scope.logs.push({ text: testString });
$scope.$apply();
};
$scope.initialize = function() {
signalRService.connection.Hub1.client.TestMethod1Hub1 = self.TestMethod;
//signalRService.connection.Hub2.client.TestMethod1Hub2 = self.TestMethod;
signalRService.initialize();
};
$scope.addHandlers = function () {
//this will call the client method cause it is set before the connection start.
signalRService.connection.Hub1.server.testMethod1("Test 1");
//This is working, so on function isn't required?
signalRService.connection.Hub1.client.TestMethod2Hub1 = self.TestMethod;
signalRService.connection.Hub1.server.testMethod2("Test 2");
//So you don't need the on method (anymore?). (By the way, this is working as well ofcourse)
signalRService.connection.Hub1.on("TestMethod2Hub1", self.TestMethod);
signalRService.connection.Hub1.server.testMethod2("Test 3");
//this doesn't work (obviously?) unless the line in the initalize method is uncommented
signalRService.connection.Hub2.client.TestMethod1Hub2 = self.TestMethod;
signalRService.connection.Hub2.server.testMethod1("Test 4");
//but this doesn't work either. Same: works if line in the initialize method is uncommented
signalRService.connection.Hub2.on("TestMethod1Hub2", self.TestMethod);
signalRService.connection.Hub2.server.testMethod1("Test 5");
//Also, I get the test 4 and test 5 twice, so the event handlers are added, not replaced.
};
}]);
In the example, the binding to the client methods happens much later after the signalR is started (In this case, by pressing the button as example, but in a live example it could be when a user navigates to a different ng-view template and with that starts a different angular controller which also depends on client methods). In the test I see I have to add a client method (dummy or not) to every hub so I can add extra client methods later at a start up of another angular controller. I wonder if this could be done otherwise, so you don't get a long list of binding dummy functions to client methods?
Also, it doesn't seems to be nessecary to use the on function, binding straight away seems to work as well after the connection is started. Perhaps this is changed in SignalR version 2.0.0
Gonna give this a shot, I think I understand what you're asking so I'll provide some guidance:
Hope this helps!