我在角度js中使用简单的信号器来通知用户,但是只有在刷新页面时我调用客户端方法,我在webapi中有服务器方法
startup.cs
[assembly: OwinStartupAttribute(typeof(signalRTest.Startup))]
namespace signalRTest
{
public partial class Startup
{
public void Configuration(IAppBuilder app)
{
app.Map("/signalr", map =>
{
map.UseCors(CorsOptions.AllowAll);
var hubConfiguration = new HubConfiguration
{
};
map.RunSignalR(hubConfiguration);
});
}
}
}
集线器课程
public class ContactHub : Hub
{
public void UserRideRequested(int id)
{
Clients.All.notifyUsers(id);
}
}
客户端 -
index.html
<html lang="en">
<head>
<script src="Scripts/jquery.signalR-2.2.0.js"></script>
<script src="http://localhost:1526/signalr/hubs"></script>
</head>
<body ng-app="ApplicationModule">
<div ng-view class="slide">
</div>
<script type="text/javascript">
var contactNotifyUsers = undefined;
jQuery.support.cors = true;
$.connection.hub.url = 'http://localhost:1526/signalr';
contactNotifyUsers = $.connection.contactHub;
</script>
</body>
控制器将id发送到服务器
var AdminController = function ($scope,$route) {
$.connection.hub.url = 'http://localhost:1526/signalr';
$.connection.hub.start().done(function () {
contactNotifyUsers.server.userRideRequested(rideId);
$route.reload();
});
}
客户端控制器
var UserController = function ($scope, $location, $http) {
$.connection.hub.url = 'http://localhost:1526/signalr';
contactNotifyUsers.client.notifyUsers = function (id) {
OpenModalForRideResponse.checkModal(id);
};
$.connection.hub.start();
}
当我重新加载页面时,此代码可以正常工作,否则它不显示错误或任何影响
答案 0 :(得分:0)
因为在应用程序路由实际分支(命中)之前,Owin Startup.CS
中的映射不会运行。我不确定你是否正在尝试一些特殊的东西,但足以配置它如下:
[assembly: OwinStartupAttribute(typeof(signalRTest.Startup))]
namespace signalRTest
{
public partial class Startup
{
public void Configuration(IAppBuilder app)
{
app.UseCors(CorsOptions.AllowAll);
app.MapSignalR();
}
}
}
默认情况下,SignalR会将请求映射到/signalr