我正在尝试在应用程序中使用 SignalR 集线器,但目前运气不佳。我已经通读了 Scott Hanselman's article , 还查看了 this博客文章展示了如何在 MVC 中实现它,基本上做同样的事情。不幸的是,我没有运气。在客户端,javascript 似乎工作正常 [除了没有任何反应] 但是当我在代码中放置断点时它表明正在调用 Controller ,而不是集线器代码。我使用的代码是这样的:
// Client side javascript:
var hooking;
$(function() {
// Setup SignalR
hooking = $.connection.hooking;
hooking.removeLead = function(ref) {
$("lead" + ref).remove();
};
$.connection.hub.start();
}
});
// Hooking.cs (placed in application root)
public class Hooking : Hub
{
public void Submit(string jsonString)
{
var serializer = new JavaScriptSerializer();
var json = serializer.Deserialize<HookingLeadResult>(jsonString);
Clients.removeLead(json.Ref); // Remove lead from client hooking windows
// update lead gen
}
}
当我稍后在我的代码中调用 hooking.submit(resultJson);
时出于某种原因它会调用我的 HookingController(即当前页面)的 Index 操作。有人知道如何从 Hooking.cs 正确调用提交函数吗?
最佳答案
您是否在 firebug 或其他嗅探 http 流量的工具中查看网络流量以确保您没有错误?这可能与路由有关。
还有一个旁注,您不需要序列化我们为您做的任何事情。只需来回发送对象即可。
当您在 mvc 应用程序中时,您需要像任何其他静态脚本一样包含 hub 脚本:
<script src="@Url.Content("~/signalr/hubs")" type="text/javascript"></script>
关于c# - 将 SignalR 与 ASP.NET MVC3 结合使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7594978/