1,SignalR2.0是支持跨域的,需要开发人员手动启用。
2,服务端在Startup文件中添加如下:
using Microsoft.Owin.Cors;//添加此引用
//add the attribute here
[assembly: OwinStartup(typeof(SignalrMvc.Startup))]
namespace SignalrMvc
{
public class Startup
{
public void Configuration(IAppBuilder app)
{
app.UseCors(CorsOptions.AllowAll);//跨域访问
app.MapSignalR();//路由访问
}
}
}
3,在客户端引用SignalR
<script src="http://localhost/Scripts/jquery.signalR-2.0.2.min.js" type="text/javascript"></script>
<script src="http://localhost/signalr/hubs" type="text/javascript"></script>
$.connection.hub.url = "http://localhost/signalr/hubs";
var hubProxy = $.connection.hubName;