David Horák
David Horák

Reputation: 5565

SignalR javascript callback not working

I have hub class:

public class ChatHub : Hub
{
    // Send message
    public void Send(string message)
    {
        Clients.addMessage(DateTime.Now.ToString("HH:mm:ss") + " " + message);
    }
}

And javascript:

// Proxy created on the fly
var chat = $.connection.chatHub;

// Declare a function on the chat hub so the server can invoke it
chat.addMessage = function(message) {
    alert("message:" + message);
    $('#chat').append('<div>' + message + '</div>');
};


$("#sendButton").click(function () {

    // Call the chat method on the server
    chat.send($('#message').val())
        .done(function () {
            console.log('Success!')
        })
        .fail(function (e) {
            console.warn(e);
        })
});

// Start the connection
$.connection.hub.start();

All conection are fine: enter image description here

If I use breakpoint in here Clients.addMessage(DateTime.Now.ToString("HH:mm:ss") + " " + message); everything is fine.

But i don't get callback on javascript function. alert("message:" + message); never executes

Upvotes: 5

Views: 3358

Answers (1)

Heather
Heather

Reputation: 2662

Did you add the client side Signalr hub?

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

Upvotes: 3

Related Questions