Cannot read property 'chatHub' of undefine

2019-08-15 12:03发布

i'm try to learn signalr and this error i'm founded it.

Cannot read property 'chatHub' of undefined.

$(document).ready(function () {
        var chat = $.connection.chatHub;
        $.connection.hub.start();
 });

and hub file is:

namespace TestSignalR.Web.Hubs
{
public class ChatHub : Microsoft.AspNet.SignalR.Hub
{
    public void Send(string msg)
    {
        ChatData chat = new ChatData();
        chat.Msg = msg;
        chat.UserName = HttpContext.Current.User.Identity.Name;
        chat.Date = "♣ at " + DateTime.Now.ToString("hh:mm tt");
        Clients.All.broadCastMessage(chat);
    }
}
}

6条回答
手持菜刀,她持情操
2楼-- · 2019-08-15 12:20

I always seem to run into this problem due to how MVC tries to be helpful and renders the jquery bundle. Then when you try to add signalr in your view it will fail because you have to add jquery in beforehand, but jquery is then loaded again by the MVC bundle. This confuses stuff it seems and causes errors. You will probably have this section in your main layout page:

@Scripts.Render("~/bundles/jquery")
@RenderSection("scripts", required: false)

So if you add your scripts to the scripts section it should make signalr happy and work fine.

@section scripts {
    <!--Reference the SignalR library. -->
    <script src="Scripts/jquery.signalR-2.0.2.min.js"></script>
    <!--Reference the autogenerated SignalR hub script. -->
    <script src="signalr/hubs"></script>
    <!--Add script to update the page and send messages.--> 
    <script type="text/javascript">
        $(function () {
            // Declare a proxy to reference the hub. 
            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>
}
查看更多
劳资没心,怎么记你
3楼-- · 2019-08-15 12:24

I had a problem with Firefox, in my case, I'm using https and in dev it does not exist, so I got the error because the exception for https was not registered. Just access the signalr server and add that.

查看更多
可以哭但决不认输i
4楼-- · 2019-08-15 12:24

I face the same problem after try this tutorial: https://docs.microsoft.com/en-us/aspnet/signalr/overview/getting-started/tutorial-getting-started-with-signalr

I recognize one instruction missing:

using System;
using System.Threading.Tasks;
using Microsoft.Owin;
using Owin;

[assembly: OwinStartup(typeof(signalChat.Startup))]

namespace signalChat
{
    public class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            app.MapSignalR(); // add this 
        }
    }
}
查看更多
叛逆
5楼-- · 2019-08-15 12:28

Probably missing this line in the <HEAD> of your document:

<script src="/signalr/hubs" type="text/javascript"></script>

Check you also have

 <script src="/Scripts/jquery.signalR-1.0.0.js"></script>

And check, using Fiddler or Chrome Developer Tools that both files are loading and that the /hubs file contains what you expect it to contain in terms of hub definitions.

查看更多
我只想做你的唯一
6楼-- · 2019-08-15 12:31

The solution for all those who had correct dll but even the solution is not working is that the function names casing gets changed.

Like my class name is TestAHub

But it is changed to testAHub.

You actually need to open this js "/signalr/hubs" to check the names.

查看更多
我只想做你的唯一
7楼-- · 2019-08-15 12:38

Check you have app.MapSignalR(); on your Startup.cs file. See more on http://www.asp.net/signalr/overview/releases/upgrading-signalr-1x-projects-to-20.

查看更多
登录 后发表回答