stato alle prese con questo errore la pasta due ore ..
TypeError: $.connection is undefined
[Break On This Error]
var chatHubClient = $.connection.chatHub;
qui è la mia Chat.cs (che si trova nella cartella di controllo - la mia ipotesi è questo è quello che sto facendo male, ma non riesco a trovare alcuna documentazione su dove metterlo o come instradarlo)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using SignalR.Hubs;
namespace SingalrTest.Controllers
{
public class Chat
{
private readonly static Lazy<Chat> _instance = new Lazy<Chat>(() => new Chat());
public static Chat Instance
{ get { return _instance.Value; } }
}
[HubName("chatHub")]
public class ChatHub : Hub
{
private readonly int TimeoutInSeconds = 30;
private readonly Chat _chat;
public ChatHub() : this(Chat.Instance) { }
private ChatHub(Chat chat)
{
this._chat = chat;
}
public void Join(string channelName)
{
System.Diagnostics.Debug.WriteLine("Someone joined " + channelName);
}
}
}
e alla fine, .. il potrebbe _Layout.cs.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>@ViewBag.Title - My ASP.NET MVC Application</title>
<link href="~/favicon.ico" rel="shortcut icon" type="image/x-icon" />
<meta name="viewport" content="width=device-width" />
@Styles.Render("~/Content/css")
@Scripts.Render("~/bundles/modernizr")
@Scripts.Render("~/Scripts/jquery-1.8.1.js")
@Scripts.Render("~/Scripts/jquery.signalR-0.5.3.js")
@Scripts.Render("~/signalr/hubs")
<script type="text/javascript">
$(document).ready(function() {
// var chatHubClient = jQuery.connection.chatHub;
var chatHubClient = $.connection.chatHub;
$.connection.chatHub.start(function() {
chatHubClient.join('TEST');
});
});
</script>
</head>
<body>
...
</body>
</html>
E se visito il/signalr/hubs posso vedere che la fonte è generata correttamente. Quindi cosa mi manca?
Sto avendo lo stesso problema. Per qualche ragione $ .connection non viene definita quando si verifica l'evento $ .ready. Ho risolto il problema eseguendo il mio codice inline anziché sull'evento pronto ... Non ho idea del perché scompaia però ... –