【问题标题】:SignalR 2.0.0.0: $.connection is undefinedSignalR 2.0.0.0:$.connection 未定义
【发布时间】:2013-10-18 07:52:35
【问题描述】:

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

这是来自网络控制台的错误:

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

Startup.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。

【问题讨论】:

  • 您的脚本包含的顺序是否正确?
  • 在 app.MapSignalR() 上设置一个调试断点,看看它实际上是在生成“/signalr/hubs”路径
  • 是的,它们都包含在内,如下所示: 是的,当我在 app.MapSignalR() 上添加断点时,我点击了它应用程序启动。但它仍然拒绝工作..
  • @MattiasHagström 我的问题似乎和你的完全一样。你是怎么解决的?

标签: c# javascript signalr


【解决方案1】:

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

【讨论】:

    【解决方案2】:

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

    [HubName("chatHub")]

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        猜你喜欢
        • 2012-06-27
        • 1970-01-01
        • 2012-09-05
        • 1970-01-01
        • 1970-01-01
        • 2021-04-07
        • 1970-01-01
        • 2012-11-08
        • 2019-06-17
        相关资源
        最近更新 更多