这是我的集线器课程:
public class ZaaloverzichtHub : Hub
{
IHubContext context = GlobalHost.ConnectionManager.GetHubContext<ZaaloverzichtHub>();
private static readonly Lazy<ZaaloverzichtHub> instance = new Lazy<ZaaloverzichtHub>(() => new ZaaloverzichtHub());
public static ZaaloverzichtHub Instance { get { return instance.Value; } }
public void Send()//PartialViewResult zaaloverzicht)
{
// Call the broadcastMessage method to update clients.
context.Clients.All.broadcastMessage("test");
}
}
这是我视图中的javascript:
$(function () {
var connection = $.connection.ZaaloverzichtHub;
connection.client.broadcastMessage = function (zaaloverzicht) {
window.alert(zaaloverzicht);
}
$.connection.hub.start();
});
我正在从我的控制器调用Send()方法
var hub = ZaaloverzichtHub.Instance;
hub.Send();
正在调用集线器中的Send()方法,但客户端没有发生任何事情。我知道客户端没有连接到集线器,因为当我创建这样的方法时:
public override System.Threading.Tasks.Task OnConnected()
{
return base.OnConnected();
}
并设置断点,断点永远不会被击中。
答案 0 :(得分:2)
SignalR的约定是在客户端使用小写命名方案,除非使用c#中的属性明确命名
var connection = $.connection.zaaloverzichtHub;//note the case change on Z to z