运行 在 MonoDevelop SignalR ChatHub 上未定义

Running on MonoDevelop SignalR ChatHub Undefined

我正在尝试使用 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 的一个特定问题。我添加了标签。

您忘记使用 SignalR 注册您的 ChatHub class。以下是我通常的做法:

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

    app.MapSignalR();
}

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

不确定这是否有帮助,但我的集线器配置没有改变:

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

你可以在这里看到更多关于我签名的信息:

似乎 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