1

This is my Signal R Client. I get this error when i run my client.(0x800a139e - JavaScript runtime error: SignalR: Error loading hubs. Ensure your hubs reference is correct, e.g. .)

The exception comes from line $.connection.hub.start

There is a ServerHub class in a folder HUBS in my Server application which runs fine.

Can anyone help me out.. Thanks

<script src="~/Scripts/jquery.signalR-2.2.0.min.js"></script>
<script src="http://localhost:39670/MySignalRServer/signalr/hubs"></script>
 var ChatHubProxy = $.hubConnection("http://localhost:39670/MySignalRServer/signalr/hubs");
        var chat = ChatHubProxy.createHubProxy('ServerHub');

    chat.on("addNewMessageToPage",function (name, message) {
        // Add the message to the page.
          $('#discussion').append('<li><strong>' + htmlEncode(name)
            + '</strong>: ' + htmlEncode(message) + '</li>');
        });
      $.connection.hub.start({jsonp:true}).done(function () {
            $('#sendmessage').click(function () {
                // Call the Send method on the hub.
                chat.server.send($('#displayname').val(),       $('#message').val());
                alert("hiii");
                // Clear text box and reset focus for next comment.
                $('#message').val('').focus();
            });
        });
kaushik reddy
  • 167
  • 3
  • 16

1 Answers1

3

Try change your code to :

<script src="~/Scripts/jquery.signalR-2.2.0.min.js"></script>
<script src="http://localhost:39670/MySignalRServer/signalr/hubs"></script>

    var chat = $.connection.ServerHub;  //here

    chat.on("addNewMessageToPage", function(name, message) {
        // Add the message to the page.
        $('#discussion').append('<li><strong>' + htmlEncode(name)
            + '</strong>: ' + htmlEncode(message) + '</li>');
    });

    $.connection.hub.start().done(function() {    //here
        $('#sendmessage').click(function() {
            // Call the Send method on the hub.
            chat.server.send($('#displayname').val(), $('#message').val());
            alert("hiii");
            // Clear text box and reset focus for next comment.
            $('#message').val('').focus();
        });
    });

And make sure that this address is good - <script src="http://localhost:39670/MySignalRServer/signalr/hubs"></script>

I always use this - <script src="/signalr/hubs"></script>

And add HubName Attribute

[HubName("ServerHub")]
public class ServerHub : Hub
{
    public string Send(string name, string message)
    {
        Clients.All.broadcastMessage(name, message);

        return null;
    }
}

or change this code :

 var chat = $.connection.ServerHub;

to

 var chat = $.connection.serverHub;

Demo project : https://github.com/czerwonkabartosz/SignalRDemo

Bartosz Czerwonka
  • 1,631
  • 1
  • 10
  • 11
  • I will try this.... My server and client are in two different projects so I have to use localhost only right? @ Bartosz Czerwonka – kaushik reddy Oct 18 '15 at 02:49
  • Yes, run two project at a time and use localhost: port from your server projects - ` – Bartosz Czerwonka Oct 18 '15 at 05:48
  • I tried that.... Its showing js runtime error:Unable to get property 'on' of undefined or null reference...... @ Bartosz Czerwonka – kaushik reddy Oct 18 '15 at 07:19
  • I added that attribute on my server side in ServerHub.cs... but still i get the same error..... my server hub is placed in a folder HUBS.... could that be a problem??? @ Bartosz Czerwonka – kaushik reddy Oct 18 '15 at 07:48
  • Make sure that you can enter the address from your browser - http://localhost:[your port]/signalr/hubs – Bartosz Czerwonka Oct 18 '15 at 07:53
  • Should i check for http://localhost:39670/signalr/hubs or http://localhost:39670/MySignalRServer/signalr/hubs ??? @ Bartosz Czerwonka – kaushik reddy Oct 18 '15 at 07:54
  • Check both addresses, if one is ok then use it. I have a suspicion that if you use 2 projects is the problem with loading the file hubs (CORS). Please add to your server something like this answer - http://stackoverflow.com/a/33144418/4599089 – Bartosz Czerwonka Oct 18 '15 at 07:59
  • Can you give me a sample working code on client and server?? @ Bartosz Czerwonka – kaushik reddy Oct 18 '15 at 08:08
  • i added the cross domain code to my server startup.cs , localhost:39670/signalr/hubs this is opening in browser without error,added the same..., still i get same exception in chat.on().. @ Bartosz Czerwonka – kaushik reddy Oct 18 '15 at 09:29
  • Hey.. Its Working now...... I just needed to add the cross domain code...I commented my signal R script reference while debugging.....Uncommenting it and adding the cross domain code worked for me... Thanks a lot @ Bartosz Czerwonka – kaushik reddy Oct 18 '15 at 09:39