$■连接未定义+ signalR 0.5.3 + MVC4 [英] $.connection is undefined + signalR 0.5.3 + MVC4

查看:87
本文介绍了$■连接未定义+ signalR 0.5.3 + MVC4的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

已与此错误挣扎贴2小时..

Been struggling with this error the paste two hours..

TypeError: $.connection is undefined
[Break On This Error]   

var chatHubClient = $.connection.chatHub;

这里是我的Chat.cs(这是在控制器文件夹 - 我的猜测是这是我在做什么错的,但我找不到在哪里把它的任何文件或如何路由吧)

here is my Chat.cs (which is in the controller folder - my guess is this is what I'm doing wrong, but I can't find any documentation on where to put it or how to route it)

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

And at last,.. the might _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 /集线器我可以看到源正确生成。所以我缺少什么biggy?

And if I visit the /signalr/hubs I can see that the source is generated correctly. So what biggy am I missing?

推荐答案

您可能已经......移动

you may have moved on...

但这里的解决方案。

您在头标记按正确的顺序所有的脚本。但 _Layout.cshtml 从Visual Studio模板具有以下在body标签的端线。

you have all your scripts in the right order in the head tag. But the _Layout.cshtml from the Visual Studio template has following lines at the end of the body tag.

@Scripts.Render("~/bundles/jquery")
@RenderSection("scripts", required: false)

这包有jQuery库,在加载时它将覆盖$对象。因此,在这个设置由SignalR任何东西,现在覆盖,并且无法访问。

This bundle has the jQuery library, which overwrites the $ object when it loads. So any 'stuff' on this setup by SignalR are now overwritten and are not accessible.

简单地使用在你的页面下面。

simply use following in your page.

@section scripts {
    @* Load your scripts here. (and remove jQuery, coz it's already loaded) *@
}

希望这有助于。

这篇关于$■连接未定义+ signalR 0.5.3 + MVC4的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆