2012-05-03 39 views
3

基本上我有3个应用程序。 Web应用程序,Nservicebus和我的集线器的信号自我托管服务器。我目前使用的信号版本.4。基本上,网页应用程序会在加载页面时启动与自己托管的服务器的连接。用户执行向服务总线发送命令的操作。然后服务总线连接到信号服务器以通知所有Web客户端。为什么我的网页无法接收信号消息

我的网页:

<script src="http://localhost:8081/test/signalr/hubs" type="text/javascript"></script> 
<script> 
    jQuery.support.cors = true; 
    var myHub; 

    $.connection.hub.url = 'http://localhost:8081/test/signalr/hubs'; 

    $(function() { 
     myHub = $.connection.userAccountHub; 

     myHub.ChangeNameUpdated = function (connId) { 
      alert("recieved signalr message");   
     }; 

     $.connection.hub.start().done(function() { 
      var myClientId = $.connection.hub.id; 
      setCookie("srconnectionid", myClientId); 
     }); 
    }); 

</script> 

我SignalR服务器:

 static void Main(string[] args) 
    { 
     Debug.Listeners.Add(new ConsoleTraceListener()); 
     Debug.AutoFlush = true; 

     string url = "http://localhost:8081/test/"; 
     var server = new Server(url); 


     server.EnableHubs(); 
     server.Start(); 


     Console.WriteLine("Server running on {0}", url); 

     while (true) 
     strong text{ 
      ConsoleKeyInfo ki = Console.ReadKey(true); 
      if (ki.Key == ConsoleKey.X) 
      { 
       break; 
      } 
     } 
    } 

我我Signalr服务器项目中集线器

public class UserAccountHub : Hub 
    { 

     public void UsersNameChanged(string passedThroughConnectionId) 
     { 
      Clients.ChangeNameUpdated(passedThroughConnectionId); 
     } 
    } 

我从我的nservicebus

 //connect to signalRServer 
     var connection = new HubConnection("http://localhost:8081/test/signalr/hubs"); 
     IHubProxy myHub = connection.CreateProxy("SignalRServer.Hubs.UserAccountHub"); 

     //Credentials are inherited from the application 
     connection.Credentials = CredentialCache.DefaultNetworkCredentials; 

     var test = message.GetHeader("signalrConnectionId"); 


     connection.Start().Wait(); 
     myHub.Invoke("UsersNameChanged", message.GetHeader("signalrConnectionId")).ContinueWith(task => 
     { 
      //for debuging purposes 
      if (task.IsFaulted) 
      { 
       Console.WriteLine(
        "An error occurred during the method call {0}", 
        task.Exception.GetBaseException()); 
      } 
      else 
      { 
       Console.WriteLine("Successfully called MethodOnServer"); 
      } 
     }); 

呼叫我打开了2个浏览器。我在第二个浏览器上启动该进程,并且只有第一个接收通知。第二个浏览器没有。

我没有看到任何突出的问题,当我运行提琴手或者。

有没有更好的方法来执行此操作,或者我错过了什么?

+1

是否有任何工作? 'var connection = new HubConnection(“http:// localhost:8081/test/signalr/hubs”);' 上述行不正确。您创建到集线器的URL像这样的连接: 'VAR连接=新HubConnection( “HTTP://本地主机:8081 /测试/”);' 同为你的JavaScript枢纽URL,它应该是: $ .connection.hub.url ='http:// localhost:8081/test/signalr'; – davidfowl

+0

它“有时”起作用,这让我觉得我正在正确地完成连接部分。如果我仅使用1个浏览器,则所有内容似乎都能正常工作(第一次),如果我尝试并执行第二个操作,它将停止接收通知。但是,如果我从头开始刷新页面,它将得到1通知,然后再没有收到。在检查出另一个项目上的提琴手之后,这看起来有点不同。该解决方案收到第一个通知后停止长轮询。 – Etch

+0

我将按照您指定的方式使用连接检出。 – Etch

回答

2

好的!大卫的评论让我朝着正确的方向前进。也注意到这是更新,没有注意到这一点。 Scroll to the bottom for the cross domain support

1)更新到版本0.5

2)修改在JavaScript我的连接信息

$.connection.hub.url = 'http://localhost:8081/test/signalr'; 

$.connection.hub.start({ transport: 'longPolling', xdomain: true }).done(function() { 
    //alert("staring hub connection: " + $.connection.hub.id); 
    setCookie("srconnectionid", $.connection.hub.id); 
}); 

3)我不得不改变HubConnection

var connection = new HubConnection("http://localhost:8081/test/"); 

4.)我不得不改变IHubProxy

IHubProxy myHub = connection.CreateProxy("UserAccountHub"); 
相关问题