signalR can't find hub method

I'm trying to write a simple signalR web app. I have a ChatHub

public class ChatHub : Hub
{
    public void BroadcastMessage(string message)
    {
        Clients.All.writeMessage(message);
    }
}

I have js

<script src="~/signalr/hubs"></script>
<script>
    $(function () {
        var chat = $.connection.chatHub;
        chat.writeMessage = function (msg) {
            $('#mainContainer').append("<h2>" + msg + "</h2>");
        }

        $.connection.hub.start();

        var valll = 0;
        var interval = setInterval(
            function () {
                chat.broadcastMessage(valll);
                valll++;
                if (valll > 10)
                    clearInterval(interval);
            },
            500
        )
    }
    );
</script>

chat is not undentified, but I got an error:

Uncaught TypeError: chat.broadcastMessage is not a function

How can I find part of code which contains error?

Upvotes: 0

Views: 616

Answers (2)

Lorien
Lorien

Reputation: 160

Have you loaded this in page or header?

<script src="@Url.Content("~/Scripts/jquery.signalR-2.2.1.min.js")"></script>

Upvotes: 0

James Ralston
James Ralston

Reputation: 1208

try this

chat.server.broadcastMessage(valll);

Upvotes: 1

Related Questions