我正在學習如何使用AngularJS和SignalR,我想知道是否可以重新啓動SignalR連接而不會丟失服務器端的connectionId。我問這個問題的原因與需要稱爲serverside的客戶端方法有關。我還沒有嘗試過任何東西,我只是在考慮這種情況,什麼是最佳實踐解決方案,我希望有人能夠思考或可能有解決方案,並想解釋它。例如:我有兩個角度控制器,controller1
和controller2
和兩個signalR集線器,hub1
和hub2
。 controller1
在打開網站時啓動,在controller1
的初始化過程中,我可以將函數綁定到在SignalR啓動之前需要在hub1
中調用的客戶端方法。這工作正常,甚至在signalR啓動後,即使signalR已啓動,我仍然可以使用on
函數將函數綁定到客戶端方法,但這可能不是祕密,因爲我可以在啓動signalR連接之前將函數綁定到客戶端方法。是否可以重新啓動/重新連接SignalR連接?
接下來,在窗體上我得到一個按鈕,該按鈕正在啓動另一個具有controller2
的div,如ng-controller
。在controller2
的初始化中,我想將函數綁定到需要在hub2
中調用的客戶端方法。但由於signalR連接已經由controller1
啓動,我不能這樣做hub2.client.AMethod = function() { }
。我在想,如果我可以重新啓動signalR連接而不丟失服務器端的connectionId,並重新啓動,還可以刷新所有客戶端方法綁定,這是否可能?如果沒有,我可以使用on
函數,即使在hub2
之前沒有綁定到客戶端方法的函數嗎?或者我必須在hub2
上將一個空函數綁定到客戶端方法,以及在我開始signalR連接之前?
編輯:我花時間設置了一個代碼示例。
我得到了2輪轂: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);
}
}
和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);
}
}
而且我得到了我的angularJS控制器:
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.
};
}]);
在這個例子中,在綁定客戶端方法在信號R啓動後發生很多事件(在這種情況下,通過按下按鈕作爲示例,但是在現場例如,當用戶導航到不同的ng-view模板並且啓動不同的角度控制器時,這也取決於客戶端方法)。在測試中,我看到我必須爲每個集線器添加一個客戶端方法(虛擬或不虛擬),以便稍後在另一個角度控制器啓動時添加額外的客戶端方法。我想知道這是否可以另外做,所以你沒有得到一個綁定虛擬函數的客戶端方法長列表?
此外,使用on函數看起來似乎沒有必要,直接綁定似乎也可以在連接啓動後起作用。也許這就是SignalR版本2.0.0改變
謝謝您花時間回答。這與我的問題並不完全相同,因此我花時間在開始的帖子中添加了一些示例代碼。也許這有助於澄清我的問題。 – Cornelis