1

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

这是来自 Web 控制台的错误:

  1. 未捕获的类型错误:无法读取未定义的属性“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();
        }
    }
}

启动.cs

using Microsoft.Owin;
using Owin;

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

前端:

<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/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;
    };

还应该提到我从 nuget 安装了 Signalr,并且我正在使用 VS2012。

4

3 回答 3

5

为我解决这个问题BundleConfig.RegisterBundles(BundleTable.Bundles);Global.asax.cs我遇到了这个问题,因为 jQuery 被包含了两次。

于 2014-01-09T00:53:48.990 回答
1

在您的 ChatHub 类定义之前放置如下元标记

[中心名称(“聊天中心”)]

于 2014-05-25T03:27:26.963 回答
0

您对 jquery 的引用是否正确?SignalR 包当前安装的是 1.6.4--

于 2013-10-30T20:18:13.760 回答