Signalr 信号器2.1.2无法从客户端调用服务器方法,但可以从服务器调用客户端
我不确定这里到底出了什么问题,这一定是一些愚蠢而简单的事情。然而,我检查了一切,尝试了几种不同的方法,都没有成功 服务器到客户端的通信工作得很好。没问题 但是当我调用notifications.server.test()时代码>我什么也得不到。娜达 我可以从服务器调用testFunction(),它就可以工作了。但是调用hub的Test()方法(它只调用testFunction)并没有什么好处 这里是中心:Signalr 信号器2.1.2无法从客户端调用服务器方法,但可以从服务器调用客户端,signalr,Signalr,我不确定这里到底出了什么问题,这一定是一些愚蠢而简单的事情。然而,我检查了一切,尝试了几种不同的方法,都没有成功 服务器到客户端的通信工作得很好。没问题 但是当我调用notifications.server.test()时我什么也得不到。娜达 我可以从服务器调用testFunction(),它就可以工作了。但是调用hub的Test()方法(它只调用testFunction)并没有什么好处 这里是中心: namespace XXXX.Hubs { [HubName("monitoring
namespace XXXX.Hubs
{
[HubName("monitoringHub")]
public class MonitoringHub : Hub
{
[HubMethodName("sendCustomers")]
public static void SendCustomers()
{
IHubContext context = GlobalHost.ConnectionManager.GetHubContext<MonitoringHub>();
context.Clients.All.updateCustomers();
}
[HubMethodName("sendDetails")]
public static void SendDetails(string SONumber)
{
IHubContext context = GlobalHost.ConnectionManager.GetHubContext<MonitoringHub>();
context.Clients.All.updateDetails(SONumber);
}
[HubMethodName("test")]
public static void Test()
{
IHubContext context = GlobalHost.ConnectionManager.GetHubContext<MonitoringHub>();
context.Clients.All.testFunction();
}
}
}
namespace XXXX.Hubs
{
[HubName(“监控中心”)]
公共类监视中心:中心
{
[HubMethodName(“发送客户”)]
公共静态无效发送客户()
{
IHubContext context=GlobalHost.ConnectionManager.GetHubContext();
context.Clients.All.updateCustomers();
}
[HubMethodName(“发送详细信息”)]
公共静态void SendDetails(字符串SONumber)
{
IHubContext context=GlobalHost.ConnectionManager.GetHubContext();
context.Clients.All.updateDetails(SONumber);
}
[HubMethodName(“测试”)]
公共静态无效测试()
{
IHubContext context=GlobalHost.ConnectionManager.GetHubContext();
context.Clients.All.testFunction();
}
}
}
和客户端代码:
@section Scripts{
<script src="/Scripts/systemdetails.js"></script>
<script src="/Scripts/jquery.signalR-2.1.2.js"></script>
<!--Reference the autogenerated SignalR hub script. -->
<script src="/signalr/hubs"></script>
<script type="text/javascript">
$(function () {
// Declare a proxy to reference the hub.
var notifications = $.connection.monitoringHub;
//debugger;
// Create a function that the hub can call to broadcast messages.
notifications.client.updateDetails = function (sonumber) {
getDetails(sonumber)
};
notifications.client.testFunction = function () {
alert("Test Function");
};
// Start the connection.
$.connection.hub.start().done(function () {
//alert("connection started")
getDetails(@Model.Customer.SONumber.ToString());
setInterval(function (){
alert("Test Call");
notifications.server.test();
}, 5000);
}).fail(function (e) {
alert(e);
});
});
function getDetails(sonumber) {
if(sonumber = (@Model.Customer.SONumber.ToString())){
var tbl = $('#systemDetails');
$.ajax({
url: '/Monitoring/GetDetails/@Model.Customer.SONumber.ToString()',
contentType: 'application/html ; charset:utf-8',
type: 'GET',
dataType: 'html',
cache: false
}).success(function (result) {
storeVis();
tbl.empty().append(result);
}).complete(function(){
recallVis();
}).error(function () {
});
}
}
</script>
}
@节脚本{
$(函数(){
//声明代理以引用中心。
var通知=$.connection.monitoringHub;
//调试器;
//创建一个中心可以调用以广播消息的函数。
notifications.client.updateDetails=函数(sonumber){
获取详细信息(sonumber)
};
notifications.client.testFunction=函数(){
警报(“测试功能”);
};
//启动连接。
$.connection.hub.start().done(函数(){
//警报(“连接已启动”)
getDetails(@Model.Customer.SONumber.ToString());
setInterval(函数(){
警报(“测试呼叫”);
notifications.server.test();
}, 5000);
}).失败(功能(e){
警报(e);
});
});
函数getDetails(sonumber){
if(sonumber=(@Model.Customer.sonumber.ToString()){
变量tbl=$(“#系统详细信息”);
$.ajax({
url:“/Monitoring/GetDetails/@Model.Customer.SONumber.ToString()”,
contentType:'应用程序/html;字符集:utf-8',
键入:“GET”,
数据类型:“html”,
缓存:false
}).成功(功能(结果){
storeVis();
tbl.empty().append(结果);
}).complete(函数(){
recallVis();
}).错误(函数(){
});
}
}
}
我的interval函数按预期发出警报,然后它应该调用返回的hub方法并调用testFunction发出第二个警报。当我试图从客户端调用hub方法时,我无法获得第二个要触发的警报,但是如果我在服务器端调用hub方法(或Clients.All.testFunction),它可以正常工作。有什么好处
在服务器端,我尝试了不同的格式来调用服务器集线器,但现在的情况是,根据我所使用的版本的文档,它应该是怎样的
notifications.client.test()
No。
notifications.test()
No。
notifications.server.test()
No.客户端无法看到您的函数,因为它们不是中心实例的一部分。它们是静态的,因此信号集线器序列化程序不会将它们写入集线器的动态javascript文件
如果其他类必须访问静态函数,则需要向调用静态函数的中心类添加非静态函数。然后,您的客户将能够访问它们