用了這個錯誤的貼2小時掙扎..
TypeError: $.connection is undefined
[Break On This Error]
var chatHubClient = $.connection.chatHub;
這裏是我的Chat.cs(這是在控制器中的文件夾 - 我的猜測是這是我在做什麼錯的,但我找不到在哪裏把它或任何文件如何路由吧)
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);
}
}
}
,最後,在..可能_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>
如果我訪問/ signalr /集線器,我可以看到源生成正確。那麼我錯過了什麼?
我有這個相同的問題。出於某種原因,$ .ready事件發生時未定義$ .connection。 我通過運行內聯代碼而不是就緒事件來修復它...我不知道它爲什麼會消失,雖然... –