Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
559 views
in Technique[技术] by (71.8m points)

javascript - SignalR $.connection is undefined

I admit it, I don't know what I'm doing.

I'm attempting to learn how to use SignalR and I'm following various samples online almost verbatim and I can't get pasted $.connection being undefined. I'm working in MVC 4.0 and trying to use either nuget-downloaded signalR js files or those from sample projects. These are my script references.

@Scripts.Render("~/Scripts/jquery-1.7.2.min.js")
@Scripts.Render("~/Scripts/knockout-2.0.0.js")
@Scripts.Render("~/Scripts/jquery.signalR.js")
@Scripts.Render("~/signalr/hubs")

The scripts seem to load - I've even put alerts at the start of them which fire, but $.connection is always undefined.

In a bizarre turn of events, I have a different project where I'm trying to use a different jQuery library and it uses $. and that object is ALSO always undefined.

I'm just looking for any possible explanation of what I might be doing wrong. Thanks very much in advance.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I got the same problem today.

Your answer pointed me in the right direction, because I had the EXACT same setup for script loading.

I had all my script tags on the top of the page (head) in the following order.

  • JQuery
  • SignalR
  • /signalr/hubs
  • my-script.js

And of course, the @Layout.cshtml was so thoughtful to add @Scripts.Render("~/bundles/jquery") at the bottom of the <body> tag.

Here's what's happening.

In this configuration, when the page loads, it loads up all the scripts in the head tag, in the order they're specified.

so it loads JQuery, then SignalR, which setup $.connection then the auto-generated hubs script which sets up the $.connection.hubName,then the site's custom script.

At this point your custom code runs. BUT your code at some point, waits for the body.onload or document.onReady before accessing the $.connection

By the time this event fires, the script bundle which the Layout template added for you at the end of body tag also downloads and executes. This bundle has the jquery library again.... which resets everything SignalR setup on $ and now your $.connection is not defined anymore.

How to Solve It

Easy fix, make sure you don't load JQuery twice, once before SignalR and once after SignalR. I moved all my scripts to load after the bundle and it fixed the issue. Best practice here is to use the 'scripts' section provided by the template.

@section scripts{
    @*/* load your scripts here. You can exclude jQuery, 
      coz it's already in the template */ *@
}

So this is not a SignalR error at all...

That's 2 hours of my life... I'll never get back ...

Hope This Helps.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...