SignalR: $.connection is undefined

2020-02-05 06:13发布

I'm using Visual Studio 2012 Ultimate RC, SignalR 0.5.1 and Jquery 1.7.2 in an MVC4 application.

I have looked at: MVC4 SignalR "signalr/hubs" 501 Not Implemented Error

But it does not affect my issue (I am using IIS Express to debug).

When I try to utilize SignalR the $.connection variable is undefined. My server side code:

[HubName("tenantHub")]
public class TenantHub : Hub
{
    ...
    void TenantChange(CrudAction action, Tenant tenant)
    {
        Clients.eventOccurred(action.ToString(), tenant);
    }
}

Client side:

$(function() { var test = $.connection.tenantHub; });

Client side SignalR/hubs is being referenced and I can see the JS code, it does not throw any errors. But referencing $.connection throws a Uncaught TypeError: Cannot read property 'tenantHub' of undefined. Also tried to do the default chat example, it gives the same error. Is SignalR unsupported when utilized in VS2012 or am I just being stupid?

9条回答
爷、活的狠高调
2楼-- · 2020-02-05 06:57

Try putting RouteTable.Routes.MapHubs(); as the very first item on Application_Start() in Global.asax.cs

I have a sample code/project here

查看更多
趁早两清
3楼-- · 2020-02-05 06:59

Try removing the BundleConfig.RegisterBundles(BundleTable.Bundles); from Global.asax.cs, and see if that helps?

查看更多
成全新的幸福
4楼-- · 2020-02-05 07:00

If none of these work for you, make sure that in your final html source not only the reference to the SignalR script appears after jQuery, but also that the version matches the actual file that you have in your scripts folder.

<script src="~/Scripts/jquery.signalR-2.0.2.js"></script> 

For instance this one matches 2.0.2 version of SignalR, if you upgrade SignalR via package then you'll have to manually edit this line.

查看更多
登录 后发表回答