Note that there are some explanatory texts on larger screens.

plurals
  1. POIs it possible to restart/reconnect a SignalR connection?
    primarykey
    data
    text
    <p>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.</p> <p>For example: I have two angularJS controllers, <code>controller1</code> and <code>controller2</code> and two signalR hubs, <code>hub1</code> and <code>hub2</code>. <code>controller1</code> is started on opening the website and in the initialisation of <code>controller1</code> I can bind a function to a client method that needs to be called in <code>hub1</code> before SignalR is started. This works fine and even after signalR is started I can still bind functions to client methods with the <code>on</code> 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.</p> <p>Next, on a form I got a button and that button is starting another div which has <code>controller2</code> as <code>ng-controller</code>. In the initialisation of <code>controller2</code> I want to bind functions to client methods that needs to be called in <code>hub2</code>. But since the signalR connection is already started by <code>controller1</code>, I can't do <code>hub2.client.AMethod = function () { }</code>. 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 <code>on</code> function even if there hasn't been a function binded to client method on <code>hub2</code> before? Or do I have to bind an empty function to a client method on <code>hub2</code> as well before I start my signalR connection?</p> <p><strong>EDIT</strong>: I took the time to set up a code example.</p> <p>I got the 2 hubs: Hub1</p> <pre><code>[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); } } </code></pre> <p>and hub2:</p> <pre><code>[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); } } </code></pre> <p>And I got my angularJS controller:</p> <pre><code>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. }; }]); </code></pre> <p>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?</p> <p>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</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload