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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14146913/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 10:51:39  来源:igfitidea点击:

SignalR cannot read property client of undefined

c#signalrsignalr-hubsignalr.client

提问by polonskyg

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

我正在尝试将 SignalR 添加到我的项目 (ASPNET MVC 4)。但我不能让它工作。

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

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

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)

This is my Index.cshtml

这是我的 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>
}

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

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

And this is map.js

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

The DLL's I see references for SignalR are:

我看到的 SignalR 引用的 DLL 是:

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

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

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

NOTEI'm following the instruction found hereon 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:

注意我正在按照此处找到的有关如何设置 Windsor Castle 以使其与 SignalR 一起使用的说明进行操作,而且似乎无法创建代理,并且出现相同的错误:

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

and like this in the client

像这样在客户端

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.

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

采纳答案by Anton Shelin

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

我通过将我的 js 代码从:更改 var myHub = $.connection.SentimentsHub;var myHub = $.connection.sentimentsHub;

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

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

回答by polonskyg

Ok, I've found the issue, one thing I needed to do was:

好的,我找到了问题,我需要做的一件事是:

These two references were missing:

缺少这两个参考:

Microsoft.AspNet.SignalR.Hosting.AspNet
Microsoft.AspNet.SignalR.Hosting.Common

Now, I included them getting nuget package: WebApiDoodle.SignalR which uses those two. My question is why those Dlls weren't added one I installed the Nuget Package: Microsoft.AspNet.SignalR -Pre?

现在,我将它们包含在使用这两个包的 nuget 包中:WebApiDoodle.SignalR。我的问题是为什么没有添加那些 Dll,因为我安装了 Nuget 包:Microsoft.AspNet.SignalR -Pre?

Bug?

漏洞?

Then I had this in my global.asax.cs

然后我在我的 global.asax.cs 中有这个

using SignalR;

so

所以

RouteTable.Routes.MapHubs();

was using that one, I needed to remove that using and use this one:

正在使用那个,我需要删除它并使用这个:

using Microsoft.AspNet.SignalR;

So the MapHubs use that one, and started to work.

所以 MapHub 使用了那个,并开始工作。

Hope this helps others. Guillermo.

希望这对其他人有帮助。吉列尔莫。

回答by GraehamF

I had the same error message and resolved the issue by fixing a typo I had in the [HubName] attribute on the hub class - it was not exactly matching the property in the client-side javascript.

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

C# hub class:

C# 集线器类:

[HubName("gameHub")]
public class GameHub : Hub
{

client-side javascript:

客户端javascript:

var foo = $.connection.gameHub;

"gameHub" must be the same.

“gameHub”必须相同。

hth

回答by Finickyflame

For those who tried to add the generated proxy file path in the bundle.

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

Do not include the "~/signalr/hubs" in your BundleConfig.cs.

不要在 BundleConfig.cs 中包含“~/signalr/hubs”

You can have the JQuery.SignalRin the bundle:

您可以在包中包含JQuery.SignalR

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

But you will need to add "/signalr/hubs"it in your view:

但是您需要在视图中添加“/signalr/hubs”

@section Scripts {
    @Scripts.Render("~/bundles/signalr")
    @Scripts.Render("/signalr/hubs")
}

I hope this helps.

我希望这有帮助。

回答by Tosh

For ASP.Net MVC folks:

对于 ASP.Net MVC 人员:

Check your _Layout.cshtml: If you are calling the jquery bundle after the @RenderBody(), you will get this error.

检查您的_Layout.cshtml:如果您在 之后调用 jquery 包@RenderBody(),您将收到此错误。

Resoultion:Just move the @Scripts.Render("~/bundles/jquery")to the head section orwrite all signalr scripts in the scripts "section"

解决方案:只需将 移动@Scripts.Render("~/bundles/jquery")到 head 部分在脚本“部分”中编写所有信号器脚本

回答by Jide Lawal

Your hub classes must be defined as public. For example:

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

class ChatHub : Hub

should actually be

实际上应该是

public class ChatHub : Hub

回答by Nohim Ys

Making the Hub class "public" solved it for me.

使 Hub 类“公开”为我解决了这个问题。

回答by Majid

You should put SignalR and jQuery scripts, in correct order:

您应该按正确的顺序放置 SignalR 和 jQuery 脚本:

<script src="/Scripts/jquery-1.6.4.min.js" ></script>

<script src="/Scripts/jquery.signalR-1.1.4.js"></script>

<script src="/signalr/hubs"></script> 

回答by An Mac

In my case, I lost Microsoft.Owin.Host.SystemWeb.dll & Microsoft.Owin.Security.dllin my project. After adding References to them, that error was solved. it's working now.

就我而言,我迷失Microsoft.Owin.Host.SystemWeb.dll & Microsoft.Owin.Security.dll在我的项目中。向它们添加引用后,该错误就解决了。它现在工作。

回答by Alireza

Make sure you add

确保你添加

app.MapSignalR();

inside startup.cs and Configuration method, like this:

在 startup.cs 和 Configuration 方法里面,像这样:

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

For Asp.net Core

对于 Asp.net Core

Add

添加

services.AddSignalR();

in ConfigureServices methode in your startup.cs

在您的 startup.cs 中的 ConfigureServices 方法中

And add

并添加

app.UseSignalR(routes =>
            {
                routes.MapHub<ChatHub>("/chatHub");
            });

in Configure methode in your startup.cs

在您的 startup.cs 中的配置方法中