2013-10-18 25 views
0

我想運行我的第一個SignalR v2項目,但沒有運氣,$ .connection是未定義的。

下面是從Web控制檯錯誤:

  1. 遺漏的類型錯誤:無法讀取未定義(匿名函數)的特性 'chatHub'
  2. ķ
  3. l.fireWith
  4. p.extend。準備
  5. d

我的樞紐:

using Microsoft.AspNet.SignalR; 

namespace InstantMessage 
{ 
    public class ChatHub : Hub 
    { 
     public void Hello() 
     { 
      Clients.All.hello(); 
     } 
    } 
} 

Startup.cs

using Microsoft.Owin; 
using Owin; 

[assembly: OwinStartup(typeof(InstantMessage.Startup))] 
namespace InstantMessage 
{ 
    public class Startup 
    { 
     public void Configuration(IAppBuilder app) 
     { 
      app.MapSignalR(); 
     } 
    } 
} 

Fontend:

<head> 
    ..... 
    <!--Script references. --> 
    <!--Reference the jQuery library. --> 
    <script src="~/Scripts/jquery-1.8.2.min.js"></script> 
    <!--Reference the SignalR library. --> 
    <script src="~/Scripts/jquery.signalR-2.0.0.js"></script> 
    <!--Reference the autogenerated SignalR hub script. --> 
    <script src="~/signalr/hubs"></script> 
</head> 
<body> 
<h2>Instant Message Demo</h2> 

<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> 


    <!--Add script to update the page and send messages.--> 
    <script type="text/javascript"> 
     $(function() { 
      // Declare a proxy to reference the hub. 
      console.log($.connection); 

      var chat = $.connection.chatHub; 
      // Create a function that the hub can call to broadcast messages. 
      chat.client.broadcastMessage = 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> 

好像在/ signalr /集線器的js代碼是正確的,chathub是存在的,文件工作的自動生成精細。

$.hubConnection.prototype.createHubProxies = function() { 
     var proxies = {}; 
     this.starting(function() { 
      // Register the hub proxies as subscribed 
      // (instance, shouldSubscribe) 
      registerHubProxies(proxies, true); 

      this._registerSubscribedHubs(); 
     }).disconnected(function() { 
      // Unsubscribe all hub proxies when we "disconnect". This is to ensure that we do not re-add functional call backs. 
      // (instance, shouldSubscribe) 
      registerHubProxies(proxies, false); 
     }); 

     proxies.chatHub = this.createHubProxy('chatHub'); 
     proxies.chatHub.client = { }; 
     proxies.chatHub.server = { 
      hello: function() { 
       return proxies.chatHub.invoke.apply(proxies.chatHub, $.merge(["Hello"], $.makeArray(arguments))); 
      } 
     }; 

     return proxies; 
    }; 

還應該提一下,我從nuget安裝Signalr,我使用VS2012。

+0

是你的腳本中的元標記包括以正確的順序? – davidfowl

+0

在app.MapSignalR()上設置調試斷點,看看是否實際上生成了「/ signalr/hubs」路徑 –

+0

是的,它們全部包含在內,如下所示: 是的,當我在app.MapSignalR()上添加一個斷點我在應用程序啓動時點擊它。但它仍然拒絕工作.. –

回答

0

您引用jquery是否正確? SignalR軟件包目前安裝1.6.4--

4

Global.asax.cs中刪除BundleConfig.RegisterBundles(BundleTable.Bundles);爲我解決了這個問題。我遇到了這個問題,因爲jQuery被包含了兩次。

1

地方像下面只是你ChatHub類定義之前

[HubName("chatHub")]