我在MVC上使用了信号器自托管,需要在另一台机器上从客户端调用它,所以我写了这样的代码:
$(function () {
jQuery.support.cors = true;
$.connection.hub.url = "http://[server external Ip]:3031/signalr";
var chat = $.connection.CustomHub;
chat.client.addMessage = function (data, IMEI) {
//SomeCode
}
}
一切顺利,但我在Firefox Firebug中出现此错误:
阻止跨源请求:同源策略禁止在http:// [服务器外部IP] /信号器/协商?读取远程资源?connectionData =%5B%7B%22name%22%3A%22customhub%22% 7D%5D&安培; clientProtocol = 1.3&安培; _ = 1400692033406。这可以通过将资源移动到同一域或启用CORS来解决。
答案 0 :(得分:3)
您必须在服务器应用程序上启用跨域,方法是在启动SignalR时安装Microsoft.Owin.Cors
软件包并调用UseCors()
(假设您使用的是SignalR 2.x)。您不需要在SignalR 2.0中指定jQuery.support.cors = true;
,实际上您应该将其删除AFAIK。
答案 1 :(得分:0)
错误似乎与网络连接有关,我们已经使用了signalR,而对集线器的Url的标识并不是必须的。
在SignalR for Sales对象的实现下面:
1-在数据库SQL Server上启用服务代理:
ALTER DATABASE BlogDemos SET ENABLE_BROKER WITH ROLLBACK IMMEDIATE ;
2-从nuget安装signalR
Install-Package Microsoft.AspNet.SignalR
3-如果不是通过nuget添加的,则添加对signalr javascript库的引用
<script src="/Scripts/jquery.signalR-2.2.1.js"></script>
<!--Reference the autogenerated SignalR hub script. -->
<script src="/signalr/hubs"></script>
4-添加Java脚本以调用集线器
$(function () {
// Declare a proxy to reference the hub.
var salesNotificationsHub = $.connection.salesHub;
// Create a function that the hub can call to broadcast messages.
salesNotificationsHub.client.updateClientsales = function () {
Search();
//alert('des nouveaux sales');
};
// Start the connection.
$.connection.hub.start().done(function () {
alert('successful connection');
}).fail(function (e) {
alert(e);
});
});
5-添加在步骤4中创建的搜索功能
function Search() {
grid.reload({ searchString: searchfields });
}
6-创建代码以从GetList函数的Grid中加载网格 控制器
grid = $("#grid").grid({
dataKey: "Codsales",
uiLibrary: "bootstrap",
dataSource:"/sales/GetList",
autoLoad: true,
columns: [
{ field: "Codsales", title: "Code Demande", width: 200, sortable: true },
{ field: "Libelsales", title: "Libellé Demande", width: 200, sortable: true },
],
pager: { enable: true, limit: 10, sizes: [10, 20, 30, 40] }
});
7-在控制器中创建GetList函数
public JsonResult GetList()
{
List<salesData> objsalesList = GetList().ToList();
return Json(objGridData, JsonRequestBehavior.AllowGet);
}
8-创建函数GetList SqlDependency对象的附加位置
public static List<salesData> GetList()
{
SqlDependency dependency = new SqlDependency();
using (SqlConnection cn = new SqlConnection(connectionString))
{
using (var command = new SqlCommand(@"SELECT Codsales, Libelsales, Datsales,DatDetailledsales FROM [dbo].sales ", cn))
{
command.Notification = null;
dependency = new SqlDependency(command);
dependency.OnChange += new OnChangeEventHandler(dependency_OnChange);
if (cn.State == ConnectionState.Closed)
cn.Open();
command.ExecuteNonQuery();
}
}
List<salesData> objList = new List<salesData>();
objList=Fetchsales(filterExpression, sortExpression, pageIndex, pageSize, out total);
rowsCount = total;
return objList;
}
private static void dependency_OnChange(object sender, SqlNotificationEventArgs e)
{
if (e.Type == SqlNotificationType.Change)
{
salesHub.Updatesales();
}
}
9-创建Hub类
public class salesHub : Hub
{
[HubMethodName("updatesales")]
public static void Updatesales()
{
IHubContext context = GlobalHost.ConnectionManager.GetHubContext<salesHub>();
context.Clients.All.updateClientsales();
}
}
10- Global.asax文件中SqlDependency的配置
protected void Application_Start()
{ //Start SqlDependency with application initialization
string connString= ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
SqlDependency.Start(connString);
}
protected void Application_End()
{
//Stop SQL dependency
string connString = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
SqlDependency.Stop(connString);
}
私下