SignalR无法读取未定义的属性客户端 [英] SignalR cannot read property client of undefined

查看:288
本文介绍了SignalR无法读取未定义的属性客户端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将SignalR添加到我的项目(ASPNET MVC 4)。但是我无法使其正常工作。

I'm trying to add SignalR to my project (ASPNET MVC 4). But I can't make it work.

在下图中,您可以看到我收到的错误。

我读了很多stackoverflow帖子,但都没有解决我的问题。

In the below image you can see the error I'm receiving. I've read a lot of stackoverflow posts but none of them is resolving my issue.

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

This is what I did so far:

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)

这是我的Index.cshtml

This is my 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>
}

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

This is my IServerHub.cs file (located inside Hubs folder)

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);
        }
    }
}

这是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();
        });
    });
});

我看到SignalR的DLL是:

The DLL's I see references for SignalR are:


  1. Microsoft.AspNet.SignalR.Core

  2. Microsoft.AspNet.SignalR.Owin

  3. Microsoft.AspNet。 SignalR.SystemWeb

任何想法如何使其工作?
因为脚本在/ Script / lib文件夹中,我是否应该进行任何更改?

Any ideas how to get it work? Should I make any change because the scripts are in /Script/lib folder?

注意
遵循此处中关于如何设置温莎城堡以使其与SignalR一起使用的说明,似乎代理无法创建,并且出现相同错误:

NOTE I'm following the instruction found here on how to set up Windsor Castle to make it work with SignalR, and again, seems that the proxy cannot be created and I'm getting the same error:

Cannot read property client of undefined

表示未创建集线器的代理

meaning that the proxy to the hub was not created

这是我在服务器中的存储方式

This is how I have it in the server

public class IncidentServerHub : Hub

并在客户端中类似

var clientServerHub = $.connection.incidentServerHub;

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

Again, I can see the dynamically created file here:

/GdpSoftware.Server.Web/signalr/hubs

那么,为什么不创建代理?

So, Why the proxy is not created?

预先感谢!!!吉列尔莫。

Thanks in advance!!! Guillermo.

推荐答案

我通过将js代码从以下位置更改来解决了该问题:
var myHub = $ .connection.sentimentsHub;

var myHub = $ .connection.sentimentsHub;

I fixed that problem by changing my js code from: var myHub = $.connection.SentimentsHub; to var myHub = $.connection.sentimentsHub;

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

So if you have some hub with class name TestHub you must use testHub(first letter is lowercase) name in js

这篇关于SignalR无法读取未定义的属性客户端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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