2016-05-16 199 views
0

我在尝试使用Monodevelop设置SignalR项目。我通过Nuget安装了SignalR软件包,并且编译了所有内容,但当我打开我的页面时,我仍然收到cannot read property of undefined在MonoDevelop上运行SignalR ChatHub未定义

我无法弄清楚我错过了什么,在这一点上我想我可能会丢失一个配置文件。

这是我有:

// 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,但是当我预览铬的背景下,有什么都没有。这是一个空文件,这可能是一切的根本原因。

这是启动文件。我放了一个断点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服务器上和工作的事情。我认为这可能是mono的一个特定问题。我加了标签。

+0

是否使用的是单声道的版本? ('mono --version') – GTHvidsten

+0

我使用的是'4.2.3'版本 – Rhs

+0

哪个SignalR DLL在您的执行文件夹中? (我必须同时使用'Microsoft.AspNet.SignalR.Core.dll'和'Microsoft.AspNet.SignalR.SystemWeb.dll') – GTHvidsten

回答

0

您忘记了使用SignalR注册您的ChatHub类。下面是我平时做:

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

    app.MapSignalR(); 
} 

后你这样做了~/signalr/hubs应该包含你所需要的

+0

你把它放在哪里? – Rhs

+0

对不起,我应该说得更清楚。我更新了代码以显示我放置的位置。 – GTHvidsten

+0

有趣。我不必将我的集线器明确添加到Web服务器。我的理解是,从Hub派生并调用MapSignalR()会做到这一点。但我可以告诉你,我已经工作了2年,并且我不在任何地方将集线器()注册到我的集线器上,也没有在任何地方创建实例。 –

0

不知道这是否可以帮助,但我有一个不变的HubConfiguation:

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

你可以在这里看到更多关于我的签名: https://stackoverflow.com/a/37262337/1322383

+0

感谢您的帮助。不幸的是,这并没有解决问题。自动生成的'〜/ signalr/hubs'仍然是空白的 – Rhs