2013-10-21 54 views
0

我正在關注this教程。在客戶端,在一個簡單的html頁面中,來自SignalR的客戶端集線器代理未定義;我錯過了什麼?SignalR客戶端集線器代理未定義

此鏈接正常工作(客戶端是在同一個解決方案的另一個asp.net mvc的項目):

http://localhost:28538/Scripts/jquery.signalR-2.0.0.min.js 
http://localhost:28538/Scripts/jquery-1.8.2.min.js 
http://127.0.0.1:9077/signalr/hubs 
http://127.0.0.1:9077/signalr/js 

我的HUB類:

class AlohaHub : Hub 
{ 
    public void Send(string name, string message) 
    { 
     Clients.All.addMessage(name, message); 
    } 
} 

啓動類(這將傳遞給WebApp.Start ):

class Startup 
{ 
    public void Configuration(IAppBuilder app) 
    { 
     app.MapSignalR(); 
    } 
} 

該應用程序的主要部分(一個Windows服務,但這是不可能的levant):

class MyAppSvc : WinSvc.ISvc 
{ 
    IDisposable _app; 
    string _url = "http://127.0.0.1:9077"; 

    public void OnShutdown() 
    { 
     _app.Dispose(); 
    } 

    public void OnStart(string[] args) 
    { 
     _app = Microsoft.Owin.Hosting.WebApp.Start<MyApp.SigR.Startup>(_url); 
    } 

    public void OnStop() 
    { 
     _app.Dispose(); 
    } 
} 

實際的html頁面;客戶端:

<!DOCTYPE html> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
    <title>SigR Sample</title> 
    <style type="text/css"> 
     .container { 
      background-color: #99CCFF; 
      border: thick solid #808080; 
      padding: 20px; 
      margin: 20px; 
     } 
    </style> 
</head> 
<body> 
    <div class="container"> 
     <input type="text" id="message" /> 
     <input type="button" id="sendmessage" value="Send" /> 
     <input type="hidden" id="displayname" /> 
     <ul id="discussion"></ul> 
    </div> 
    <script src="Scripts/jquery-1.8.2.min.js" type="text/javascript"></script> 
    <script src="Scripts/jquery.signalR-2.0.0.min.js" type="text/javascript"></script> 
    <script src="http://127.0.0.1:9077/signalr/hubs" type="text/javascript"></script> 
    <script type="text/javascript"> 
     $(function() { 
      //Set the hubs URL for the connection 
      $.connection.hub.url = "http://127.0.0.1:9077/signalr"; 

      $.connection.hub.logging = true; 

      // Declare a proxy to reference the hub. 
      var chat = $.connection.alohaHub; 
      alert(chat); 
      // Create a function that the hub can call to broadcast messages. 
      chat.client.addMessage = function (name, message) { 
       // Html encode display name and message. 
       var encodedName = $('<div />').text(name).html(); 
       var encodedMsg = $('<div />').text(message).html(); 
       // Add the message to the page. 
       $('#discussion').append('<li><strong>' + encodedName 
        + '</strong>:&nbsp;&nbsp;' + encodedMsg + '</li>'); 
      }; 
      // Get the user name and store it to prepend to messages. 
      $('#displayname').val(prompt('Enter your name:', '')); 
      // Set initial focus to message input box. 
      $('#message').focus(); 
      // Start the connection. 
      $.connection.hub.start().done(function() { 
       $('#sendmessage').click(function() { 
        // Call the Send method on the hub. 
        chat.server.send($('#displayname').val(), $('#message').val()); 
        // Clear text box and reset focus for next comment. 
        $('#message').val('').focus(); 
       }); 
      }); 
     }); 
    </script> 
</body> 
</html> 
+0

我猜這是由於跨域問題...你在同一個本地端口上託管SignalR嗎? –

+0

否;端口是不同的。所以我添加了 'to web.config;但沒有成功(或者我不瞭解這個話題)。 –

回答

4

您需要在服務器上啓用CORS支持以使跨域正常工作(我還將列出如何啓用jsonp)。

啓用一個Cors:

  1. 通過的NuGet安裝Microsoft ASP.NET跨來源支持(Microsoft.Owin.Cors)
  2. 添加到您的啓動文件(地圖signalr調用之前) :

app.UseCors(CorsOptions.AllowAll); // You can modify the CorsOptions 

啓用JSONP:

修改你的 「MapSignalR」 在啓動文件通過:

app.MapSignalR(new HubConfiguration 
{ 
    EnableJSONP = true 
}); 

要做到兩者一起,你可以這樣做:

app.UseCors(CorsOptions.AllowAll) 
    .MapSignalR(new HubConfiguration 
    { 
     EnableJSONP = true 
    }); 

記住啓用這些跨域SignalR服務器上的功能會將其暴露給潛在的安全漏洞。

+0

我不得不安裝Microsoft.Owin.Cors,而不是Microsoft.AspNet.Cors(我認爲這是一個SignalR 2.x的東西) – Parker

相關問題