SignalR 2.0.0.0:$ .connection未定义

Mat*_*röm 1 javascript c# signalr

我正在尝试运行我的第一个SignalR v2项目,但没有运气,$ .connection未定义.

以下是来自Web控制台的错误:

  1. 未捕获的TypeError:无法读取undefined(匿名函数)的属性'chatHub'
  2. ķ
  3. l.fireWith
  4. p.extend.ready
  5. d

我的中心:

using Microsoft.AspNet.SignalR;

namespace InstantMessage
{
    public class ChatHub : Hub
    {
        public void Hello()
        {
            Clients.All.hello();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

Startup.cs

using Microsoft.Owin;
using Owin;

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

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>
Run Code Online (Sandbox Code Playgroud)

似乎/ signalr/hubs中的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;
    };
Run Code Online (Sandbox Code Playgroud)

还应该提一下,我从nuget安装了Signalr,我正在使用VS2012.

mas*_*sty 5

BundleConfig.RegisterBundles(BundleTable.Bundles);Global.asax.cs我那里删除固定的.我遇到了这个问题因为jQuery被包含了两次.