Cannot read property 'chatHub' of undefine

2019-08-15 11:59发布

问题:

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

回答1:

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.



回答2:

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:

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.



回答4:

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.



回答5:

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.



回答6:

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