kaushik reddy
kaushik reddy

Reputation: 167

Connecting Client Server in ASP.net MVC Signal R

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

Upvotes: 1

Views: 2247

Answers (1)

Bartosz Czerwonka
Bartosz Czerwonka

Reputation: 1651

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

Upvotes: 3

Related Questions