2012-11-02 106 views
1

我正在关注"Broadcasting over a Hub from outside of a Hub",但我的客户端浏览器没有收到任何消息。没有错误。这就像signalR不知道我的浏览器何时需要拉取hubContext并发送消息。如何使用SignalR从服务器调用客户端?

但是,当从客户端连接到集线器时,我的集线器按照预期行事。

我的枢纽:从其他地点和时间

[HubName("myHub")] 
public class MyHub : Hub 
{   
    public void SaySomething(string message) 
    { 
     Clients.say(message); 
    } 

    public void SayHelloWorld() 
    { 
     Clients.say("hello world"); 
    } 

} 

代码在服务器:

var context = GlobalHost.ConnectionManager.GetHubContext<MyHub>(); 
context.Clients.say(message); 

而我的客户端脚本:

<script src="Scripts/jquery.signalR-0.5.3.js" type="text/javascript" > </script> 
<script src="signalr/hubs"> </script> 

<script type="text/javascript"> 
    var hub = $.connection.myHub; 

    $.extend(hub, { 
     Say: function(message) { 
      alert(message); //this only works when the sayHelloWorld() method is executed 
     } 
    }); 

    $.connection.hub.start() 
     .done(function(){ 
      hub.sayHelloWorld(); //this works 
     }); 
</script> 
+0

这段代码适合我。您正在使用哪种浏览器,以及您从哪里尝试在集线器之外进行广播? –

+0

我在Windows上使用Chrome 22.0.1229.94 m。我试过从我的代码中的几个不同地方进行广播,从我的UI层(使用Nancy),我的域服务层,以及我能想到的任何地方。 –

+0

@akoeplinger您说代码适合您。我的工作也很重要。你还可以使用ConnectionManager使用广播来调用say方法吗?我刚刚开始并根据SignalR wiki重新构建解决方案,并获得同样的结果。我可以从客户端与服务器进行通信(允许服务器到客户端在同一个实例中),但是我不能广播......没有错误,没有消息。 –

回答

0

如果您使用的是最新的NuGet包(v 1.0.1)请尝试:

$.extend(hub.client, { 
     say: function(message) { 
      alert(message); //this only works when the sayHelloWorld() method is executed 
     } 

和:

$.connection.hub.start() 
     .done(function(){ 
      hub.server.sayHelloWorld(); //this works 
     }); 

SignalR定义为中心的客户端和服务器的性能,我试图从视频MoveShape样品,并面临着同样的问题。

希望它有帮助,

相关问题