C# 使用角向进路信号机后,该信号机不工作
问题是,如果我首先加载“#/chat”,我的信号R chat工作正常。但是,如果我先加载“#/”然后路由到“#/chat”,我的聊天功能将无法正常工作 我对信号R非常陌生,所以实际上我不知道这个问题。 这是我的密码: app.jsC# 使用角向进路信号机后,该信号机不工作,c#,angularjs,asp.net-mvc,signalr,signalr-hub,C#,Angularjs,Asp.net Mvc,Signalr,Signalr Hub,问题是,如果我首先加载“#/chat”,我的信号R chat工作正常。但是,如果我先加载“#/”然后路由到“#/chat”,我的聊天功能将无法正常工作 我对信号R非常陌生,所以实际上我不知道这个问题。 这是我的密码: app.js module.config(function ($routeProvider) { $routeProvider.when("/", { controller: "ItemsController", te
module.config(function ($routeProvider) {
$routeProvider.when("/", {
controller: "ItemsController",
templateUrl: "/templates/itemsView.html"
});
$routeProvider.when("/item/:id", {
controller: "SingleItemController",
templateUrl: "/templates/singleItemView.html"
});
$routeProvider.when("/login", {
controller: "LoginController",
templateUrl: "/templates/loginView.html"
});
$routeProvider.when("/chat", {
controller: "ChatController",
templateUrl: "/templates/chatView.html"
});
$routeProvider.otherwise({ redirectTo: "/" });
});
$(function () {
$.connection.hub.logging = true; //for debugg
$.connection.hub.start();
});
$.connection.hub.error(function (err) {
console.log('An error occurred: ' + err);
});
angular.module('ogateApp').value("chat", $.connection.chat);
[HubName("chat")]
public class ChatHub : Hub
{
public void SendMessage(string message)
{
Clients.All.newMessage(message);
}
}
module.controller("ChatController", function ($scope, chat) {
$scope.messages = [];
$scope.sendMessage = function() {
chat.server.sendMessage($scope.newMessage);
$scope.newMessage = "";
};
chat.client.newMessage = function (message) {
$scope.messages.push({ message: message });
$scope.$apply();
console.log(message);
};
});
ChatHub.cs
module.config(function ($routeProvider) {
$routeProvider.when("/", {
controller: "ItemsController",
templateUrl: "/templates/itemsView.html"
});
$routeProvider.when("/item/:id", {
controller: "SingleItemController",
templateUrl: "/templates/singleItemView.html"
});
$routeProvider.when("/login", {
controller: "LoginController",
templateUrl: "/templates/loginView.html"
});
$routeProvider.when("/chat", {
controller: "ChatController",
templateUrl: "/templates/chatView.html"
});
$routeProvider.otherwise({ redirectTo: "/" });
});
$(function () {
$.connection.hub.logging = true; //for debugg
$.connection.hub.start();
});
$.connection.hub.error(function (err) {
console.log('An error occurred: ' + err);
});
angular.module('ogateApp').value("chat", $.connection.chat);
[HubName("chat")]
public class ChatHub : Hub
{
public void SendMessage(string message)
{
Clients.All.newMessage(message);
}
}
module.controller("ChatController", function ($scope, chat) {
$scope.messages = [];
$scope.sendMessage = function() {
chat.server.sendMessage($scope.newMessage);
$scope.newMessage = "";
};
chat.client.newMessage = function (message) {
$scope.messages.push({ message: message });
$scope.$apply();
console.log(message);
};
});
ChatController.js
module.config(function ($routeProvider) {
$routeProvider.when("/", {
controller: "ItemsController",
templateUrl: "/templates/itemsView.html"
});
$routeProvider.when("/item/:id", {
controller: "SingleItemController",
templateUrl: "/templates/singleItemView.html"
});
$routeProvider.when("/login", {
controller: "LoginController",
templateUrl: "/templates/loginView.html"
});
$routeProvider.when("/chat", {
controller: "ChatController",
templateUrl: "/templates/chatView.html"
});
$routeProvider.otherwise({ redirectTo: "/" });
});
$(function () {
$.connection.hub.logging = true; //for debugg
$.connection.hub.start();
});
$.connection.hub.error(function (err) {
console.log('An error occurred: ' + err);
});
angular.module('ogateApp').value("chat", $.connection.chat);
[HubName("chat")]
public class ChatHub : Hub
{
public void SendMessage(string message)
{
Clients.All.newMessage(message);
}
}
module.controller("ChatController", function ($scope, chat) {
$scope.messages = [];
$scope.sendMessage = function() {
chat.server.sendMessage($scope.newMessage);
$scope.newMessage = "";
};
chat.client.newMessage = function (message) {
$scope.messages.push({ message: message });
$scope.$apply();
console.log(message);
};
});
我认为您的相对js文件路径中有一个错误
<script src="Scripts/jquery-1.10.2.min.js"></script>
<script src="Scripts/jquery.signalR-2.1.0.min.js"></script>
<script src="signalr/hubs"></script>
尝试使用绝对url进行测试:
<script src="http://localhost:your_port/signalr/hubs"></script>
在js调试期间是否有异常?我能看看你的html页面吗(带js参考)?调试时,我发现集线器仍然连接到一个过时的控制器,该控制器超出了范围。SignalR没有为您导航回我们的页面时创建的控制器的新实例重新启动集线器 为了解决这个问题,如果您在更改路由时停止了集线器,那么当您导航回初始页面时,集线器将再次正确启动 以下代码修复了该问题:
app.run([
'$rootScope', function ($rootScope, security) {
$rootScope.$on('$routeChangeStart', function (event, currRoute, prevRoute) {
$.connection.hub.stop();
});
}]);
有关详细信息,请参见本章