2014-01-29 48 views
1

我想使用SignalR與跨域,但我在調用啓動函數時收到錯誤消息。錯誤消息是"Uncaught TypeError: Cannot call method 'start' of undefined "使用SignalR與跨域

我使用的代碼 服務器端:

[裝配:OwinStartup(typeof運算(SignalRChat.Startup))]

namespace SignalRChat 
{ 
    public class Startup 
    { 
     public void Configuration(IAppBuilder app) 
     { 
      app.Map("/signalr", map => 
      {    
       map.UseCors(CorsOptions.AllowAll); 
       var hubConfiguration = new HubConfiguration 
       {     
        EnableJSONP = true 
       };    
       map.RunSignalR(hubConfiguration); 
      }); 
     } 
    } 
} 




    Client side code. 



<!DOCTYPE html> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
    <title></title> 
    <script src="/Scripts/jquery-1.6.4.min.js"></script> 
    <script src="/Scripts/jquery.signalR-1.1.4.min.js"></script> 
</head> 
<body> 
    <div></div> 
    <script type="text/javascript"> 
    var connection = $.connection.hub.url ='http://localhost:9370/signalr';  
    connection.hub.start() 
     .done(function() { 
      alert('Now connected, connection ID=' + connection.id); 
     }); 
    </script> 
    </body> 
    </html> 
+2

請您可以添加的JavaScript,尤其在你的集線器/連接上調用start()? – penderi

回答

0

我相信你應該啓用像服務器跨域這一點,SignalR 1.x的

var config = new HubConfiguration 
    { 
     EnableCrossDomain = true 
    }; 

對於singalR 2.X看here

+0

它顯示錯誤......錯誤信息是「Microsoft.AspNet.SignalR.HubConfiguration」不包含'EnableCrossDomain'的定義「 –

+0

您正在使用哪個版本的signalR? –

+0

我使用SignalR 2.0。 –

1

我很遲,但只是瀏覽安博SignalR,以及剛剛發現這個問題了,所以現在回答吧..

connection.hub.start() 
     .done(function() { 
      alert('Now connected, connection ID=' + connection.id); 
     }); 

錯的是你開始樞紐connection.hub.start() ..但在實際需要啓動連接而不是集線器connection.start()

connection.start() 
     .done(function() { 
      alert('Now connected, connection ID=' + connection.id); 
     }); 

,如果你想跨域SignalR這是工作代碼的形式我的項目..

  var con = $.hubConnection('http://localhost:50000/signalR');         
      var hub = con.createHubProxy('DataExchangeHub');   

      hub.on('OnMsgReceiveAll', function (message) { 
       $('#message1').append('<li>' + message + '</li>'); 
      }); 
      hub.on('OnMsgReceiveClient', function (message) { 
       $('#message2').append('<li>' + message + '</li>'); 
      }); 
      hub.on('OnMsgReceiveServer', function (message) { 
       $('#message3').append('<li>' + message + '</li>'); 
      }); 

      con.start({ jsonp: true}).done(function() { 
       $('#sendToAll').click(function() { 
        hub.invoke('BroadcastToAll', $('#msg').val()); 
       }); 
       $('#sendToClient').click(function() { 
        hub.invoke('BroadcastToClient', $('#msg').val()); 
       }); 
       $('#sendToServer').click(function() { 
        hub.invoke('BroadcastToServer', $('#msg').val()); 
       }); 
      }); 

     });