$ .hubConnection undefined

I am using SignalR version 2.2.0. With the code below, I get an exception indicating that $ does not define hubConnection. I know that I have script paths correctly, because I can set breakpoints in the signalr script and see what they hit. I even stepped to where the hubConnection function is defined, and yet I get an exception. What am I doing wrong?

<script type="text/javascript" src="~/scripts/jquery-1.10.2.min.js"></script>
<script type="text/javascript" src="~/scripts/jquery.signalR-2.2.0.js"></script>
<script type="text/javascript">
    $(function() {
        initAvailabilitySignalR("D420A92E163C43AB85CE342D5C10106B");
    });

    function initAvailabilitySignalR(tenantId) {
        try {
            var connection = $.hubConnection();

            var proxy = connection.createHubProxy('SignalRHub');

            proxy.on('AvailabilityChangedNotification', function(message) {
                try {
                    alert(message.UserId + " changed from " + message.OldAvailabilityId + " to " + message.NewAvailabilityId);
                } catch(ex) {
                    alert("Exception[On]: " + ex);
                }
            });

            connection.start().done(function() {
                try {
                    proxy.invoke("Connect", tenantId, "SOMEDOMAIN\\someuser");
                } catch(ex) {
                    alert("Exception: " + ex);
                }
            });
        } catch(ex) {
            alert("Exception: " + ex);
        }
    };
</script>
+4
source share
2 answers

I found a problem. I forgot that ASP.NET MVC displays jQuery at the bottom of the page. I moved the render statement to the top of the page and removed the explicit jQuery declaration from the view and it works now.

+2
source

You need to also include this script tag for SignalR to work:

<script src="~/signalr/hubs"></script>
0

All Articles