SignalR无法读取未定义的属性客户端

pol*_*kyg 48 c# signalr signalr-hub signalr.client

我正在尝试将SignalR添加到我的项目中(ASPNET MVC 4).但我不能让它发挥作用.

在下图中,您可以看到我收到的错误. 图片 我已经阅读了很多stackoverflow帖子,但没有一个是解决我的问题.

这是我到目前为止所做的:

1) Ran Install-Package Microsoft.AspNet.SignalR -Pre
2) Added RouteTable.Routes.MapHubs(); in Global.asax.cs Application_Start()
3) If I go to http://localhost:9096/Gdp.IServer.Web/signalr/hubs I can see the file content
4) Added <modules runAllManagedModulesForAllRequests="true"/> to Web.Config
5) Created folder Hubs in the root of the MVC application
6) Moved jquery and signalR scripts to /Scripts/lib folder (I'm not using jquery 1.6.4, I'm using the latest)
Run Code Online (Sandbox Code Playgroud)

这是我的Index.cshtml

<h2>List of Messages</h2>

<div class="container">
    <input type="text" id="message" />
    <input type="button" id="sendmessage" value="Send" />
    <input type="hidden" id="displayname" />
    <ul id="discussion">
    </ul>
</div>

@section pageScripts
{
    <!--Reference the SignalR library. -->
    <script src="@Url.Content("~/Scripts/jquery.signalR-1.0.0-rc1.min.js")" type="text/javascript"></script>
    <!--Reference the autogenerated SignalR hub script. -->
    <script type="text/javascript" src="~/signalr/hubs"></script>
    <script src="@Url.Content("~/Scripts/map.js")" type="text/javascript"></script>
}
Run Code Online (Sandbox Code Playgroud)

这是我的IServerHub.cs文件(位于Hubs文件夹中)

namespace Gdp.IServer.Ui.Web.Hubs
{
    using Microsoft.AspNet.SignalR.Hubs;
    [HubName("iServerHub")]
    public class IServerHub : Hub
    {
        public void Send(string name, string message)
        {
            Clients.All.broadcastMessage(name, message);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

这是map.js

$(function () {

    // Declare a proxy to reference the hub. 
    var clientServerHub = $.connection.iServerHub;

    // Create a function that the hub can call to broadcast messages.
    clientServerHub.client.broadcastMessage = function (name, message) {
        $('#discussion').append('<li><strong>' + name + '</strong>:&nbsp;&nbsp;' + 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 () {
            // Html encode display name and message. 
            var encodedName = $('<div />').text($('#displayname').val()).html();
            var encodedMsg = $('<div />').text($('#message').val()).html();

            // Call the Send method on the hub. 
            clientServerHub.server.send(encodedName, encodedMsg);

            // Clear text box and reset focus for next comment. 
            $('#message').val('').focus();
        });
    });
});
Run Code Online (Sandbox Code Playgroud)

DLL我看到SignalR的参考文献是:

  1. Microsoft.AspNet.SignalR.Core
  2. Microsoft.AspNet.SignalR.Owin
  3. Microsoft.AspNet.SignalR.SystemWeb

任何想法如何让它工作?我应该进行任何更改,因为脚本位于/ Script/lib文件夹中吗?

注意 我遵循此处的说明如何设置Windsor Castle以使其与SignalR一起使用,并且再次,似乎无法创建代理并且我得到相同的错误:

Cannot read property client of undefined
Run Code Online (Sandbox Code Playgroud)

意味着未创建集线器的代理

这就是我在服务器中的方式

public class IncidentServerHub : Hub
Run Code Online (Sandbox Code Playgroud)

并在客户端这样

var clientServerHub = $.connection.incidentServerHub;
Run Code Online (Sandbox Code Playgroud)

再次,我可以在这里看到动态创建的文件:

/GdpSoftware.Server.Web/signalr/hubs
Run Code Online (Sandbox Code Playgroud)

那么,为什么没有创建代理?

提前致谢!!!吉列尔莫.

Ant*_*lin 78

我通过改变我的js代码来解决这个问题: var myHub = $.connection.SentimentsHub; to var myHub = $.connection.sentimentsHub;

因此,如果您有一个类名为TestHub的集线器,则必须在js中使用testHub(首字母是小写)名称


Fin*_*ame 34

对于那些试图在捆绑中添加生成的代理文件路径的人.

不要在BundleConfig.cs中包含"〜/ signalr/hubs".

你可以在bundle中拥有JQuery.SignalR:

bundles.Add(new ScriptBundle("~/bundles/signalr").Include(
                  "~/Scripts/jquery.signalR-{version}.js"));
Run Code Online (Sandbox Code Playgroud)

但是您需要在视图中添加"/ signalr/hubs":

@section Scripts {
    @Scripts.Render("~/bundles/signalr")
    @Scripts.Render("/signalr/hubs")
}
Run Code Online (Sandbox Code Playgroud)

我希望这有帮助.


Gra*_*amF 19

我有相同的错误消息,通过修复我在集线器类的[HubName]属性中的拼写错误解决了问题 - 它与客户端javascript中的属性不完全匹配.

C#hub类:

[HubName("gameHub")]
public class GameHub : Hub
{
Run Code Online (Sandbox Code Playgroud)

客户端javascript:

var foo = $.connection.gameHub;
Run Code Online (Sandbox Code Playgroud)

"gameHub"必须相同.

心连心


小智 7

对于ASP.Net MVC人员:
检查_Layout.cshtml
如果在"@RenderBody()"之后调用jquery包,则会出现此错误.
结果:只需将"@ Scripts.Render("〜/ bundles/jquery")"移动到头部 在脚本中编写所有信号器脚本"部分"


小智 5

您的集线器类必须定义为公共类.例如:

class ChatHub : Hub
Run Code Online (Sandbox Code Playgroud)

应该是

public class ChatHub : Hub
Run Code Online (Sandbox Code Playgroud)