【问题标题】:Running on MonoDevelop SignalR ChatHub Undefined在 MonoDevelop SignalR ChatHub 上运行未定义
【发布时间】:2016-06-01 03:47:54
【问题描述】:

我正在尝试使用Monodevelop 设置SignalR 项目。我通过Nuget 安装了SignalR 软件包,一切都编译了,但是当我点击我的页面时,我不断收到cannot read property of undefined

我无法弄清楚我缺少什么,此时我想我可能缺少配置文件。

这是我所拥有的:

// the view
@section scripts {
    <script src="~/Scripts/jquery-2.2.3.min.js"></script>
    <script src="~/Scripts/jquery.signalR-2.2.0.min.js"></script>
    <!--Reference the autogenerated SignalR hub script. -->
    <script src="~/signalr/hubs" type="text/javascript"></script>
    <!--SignalR script to update the chat page and send messages.--> 
    <script>
        $(function () {
            // Reference the auto-generated proxy for the hub.  
            var chat = $.connection.chatHub;
            // Create a function that the hub can call back to display messages.

            console.log(chat);

            chat.client.addNewMessageToPage = function (name, message) {
                // Add the message to the page. 

                console.log('here');

                $('#discussion').append('<li><strong>' + htmlEncode(name) 
                    + '</strong>: ' + htmlEncode(message) + '</li>');
            };
            // Get the user name and store it to prepend to messages.
            $('#displayname').val(prompt('Enter your name:', ''));
            // Set initial focus to message input box.  
            $('#message').focus();
            // Start the connection.
            $.connection.hub.start().done(function () {
                $('#sendmessage').click(function () {
                    // Call the Send method on the hub. 
                    chat.server.send($('#displayname').val(), $('#message').val());
                    // Clear text box and reset focus for next comment. 
                    $('#message').val('').focus();
                });
            });
        });
        // This optional function html-encodes messages for display in the page.
        function htmlEncode(value) {
            var encodedValue = $('<div />').text(value).html();
            return encodedValue;
        }
    </script>
}

~/signalr/hubs 似乎没有 404,但是当我在 chrome 中预览上下文时,那里什么也没有。这是一个空文件,这可能是一切的根本原因。

这是启动文件。我在configuration 方法中设置了一个断点,它确实命中了该代码。

using System;
using Microsoft.Owin;
using Owin;

[assembly: OwinStartup(typeof(SignalRChat.Startup))]
namespace SignalRChat
{
    public class Startup
    {
        public Startup ()
        {
        }

        public void Configuration (IAppBuilder app)
        {
            app.MapSignalR ();
        }
    }
}

这里是枢纽:

using System;
using Microsoft.AspNet.SignalR;
using Microsoft.AspNet.SignalR.Hubs;

namespace SignalRChat
{
    [HubName("chatHub")]
    public class ChatHub : Hub
    {
        public void Send (string name, string message)
        {
            Clients.All.addNewMessageToPage (name, message);
        }

        public ChatHub ()
        {
        }
    }
}

编辑:在 IIS 服务器上的 Visual Studios 上运行此代码,一切正常。我认为这可能是mono 的特定问题。我添加了标签。

【问题讨论】:

  • 您使用的是哪个版本的 Mono? (mono --version)
  • 我正在使用版本4.2.3
  • 执行文件夹中有哪些 SignalR DLL? (我必须同时使用Microsoft.AspNet.SignalR.Core.dllMicrosoft.AspNet.SignalR.SystemWeb.dll
  • 我同时拥有Microsoft.AspNet.SignalR.Core.DllMicrosoft.AspNet.SignalR.SystemWeb.dll

标签: c# mono signalr


【解决方案1】:

您忘记向 SignalR 注册 ChatHub 类。这是我通常的做法:

public void Configure(IAppBuilder app)
{
    GlobalHost.DependencyResolver.Register(typeof(ChatHub), () =>
    {
        ChatHub hub = new ChatHub();
        return hub;
    });

    app.MapSignalR();
}

完成此操作后,~/signalr/hubs 应该包含您需要的内容

【讨论】:

  • 你把那个放在哪里?
  • 对不起,我应该更清楚地说明这一点。我已经更新了代码以显示我放置它的位置。
  • 有趣。我不必将集线器显式添加到 Web 服务器。我的理解是从 Hub 派生并调用 MapSignalR() 可以做到这一点。但我可以告诉你,我的已经工作了 2 年,我没有在任何地方调用我的集线器上的 Register(),也没有在任何地方创建它的实例。
  • 感谢您的帮助。不幸的是,这并没有解决问题,“~/signalr/hubs”仍然是一个空文件。
【解决方案2】:

不确定这是否有帮助,但我的 HubConfiguation 没有改变:

public void Configuration(IAppBuilder app)
{
    HubConfiguration config = new HubConfiguration();
    config.EnableJSONP = true;
    app.MapSignalR(config);
}

您可以在此处查看有关我的签名的更多信息: https://stackoverflow.com/a/37262337/1322383

【讨论】:

  • 感谢您的帮助。不幸的是,这并没有解决问题。自动生成的~/signalr/hubs 还是空白
【解决方案3】:

似乎 SignalR 仅在自托管时有效。

找到这两个链接。我还没有尝试过,但这可能是解决方案。

http://forums.asp.net/t/1924358.aspx?Signalr+on+mono http://social.technet.microsoft.com/wiki/contents/articles/15267.running-signalr-on-mono.aspx

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-08
    • 2012-06-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多