【问题标题】:SignalR not generating hub classSignalR 未生成集线器类
【发布时间】:2013-10-09 02:30:50
【问题描述】:

我遇到了一些基本配置问题,试图让我的第一个 SignalR 应用程序正常工作。它正在生成一个代理,但其中没有集线器,我什至无法调用 createHubProxy(),由于某种原因,该函数在我的代理中不存在。

我在 Application_start 中调用 RouteTable.Routes.MapHubs(),我也尝试在 web.config 中进行设置。我做错了什么?

这是一个普通的“ol Web Forms”应用程序。我的aspx代码:

<%@ Page Language="C#" CodeFile="blogroll3.aspx.cs" Inherits="blogroll3" %>

<!DOCTYPE html>
<html>
<head id="Head1" runat="server">
<script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.10.2.min.js"></script>
    <script src="/scripts/jquery.signalR-1.1.3.js"></script>
    <script src='<%: ResolveClientUrl("~/signalr/hubs") %>'></script>
<script src="blogroll3.js"></script>
</head>
<body>
</body>
</html>

代码隐藏:

using System;
using System.Web.UI;
using Microsoft.AspNet.SignalR;
using Microsoft.AspNet.SignalR.Hubs;

public partial class blogroll3 : Page
{
    public class ProgressHub : Hub
    {
        public void setProgress()
        {
        }
    }
}

我的客户端js代码:

//this is not working, progressHub is undefined:
//var proghub = $.connection.progressHub;

//this code fails on createHubProxy(), it doesn't exist.
var connection = $.hubConnection();
var proghub = $.connection.createHubProxy('progressHub');

这是它生成的代理:

/*!
 * ASP.NET SignalR JavaScript Library v1.1.3
 * http://signalr.net/
 *
 * Copyright Microsoft Open Technologies, Inc. All rights reserved.
 * Licensed under the Apache 2.0
 * https://github.com/SignalR/SignalR/blob/master/LICENSE.md
 *
 */

/// <reference path="..\..\SignalR.Client.JS\Scripts\jquery-1.6.4.js" />
/// <reference path="jquery.signalR.js" />
(function ($, window) {
    /// <param name="$" type="jQuery" />
    "use strict";

    if (typeof ($.signalR) !== "function") {
        throw new Error("SignalR: SignalR is not loaded. Please ensure jquery.signalR-x.js is referenced before ~/signalr/hubs.");
    }

    var signalR = $.signalR;

    function makeProxyCallback(hub, callback) {
        return function () {
            // Call the client hub method
            callback.apply(hub, $.makeArray(arguments));
        };
    }

    function registerHubProxies(instance, shouldSubscribe) {
        var key, hub, memberKey, memberValue, subscriptionMethod;

        for (key in instance) {
            if (instance.hasOwnProperty(key)) {
                hub = instance[key];

                if (!(hub.hubName)) {
                    // Not a client hub
                    continue;
                }

                if (shouldSubscribe) {
                    // We want to subscribe to the hub events
                    subscriptionMethod = hub.on;
                }
                else {
                    // We want to unsubscribe from the hub events
                    subscriptionMethod = hub.off;
                }

                // Loop through all members on the hub and find client hub functions to subscribe/unsubscribe
                for (memberKey in hub.client) {
                    if (hub.client.hasOwnProperty(memberKey)) {
                        memberValue = hub.client[memberKey];

                        if (!$.isFunction(memberValue)) {
                            // Not a client hub function
                            continue;
                        }

                        subscriptionMethod.call(hub, memberKey, makeProxyCallback(hub, memberValue));
                    }
                }
            }
        }
    }

    $.hubConnection.prototype.createHubProxies = function () {
        var proxies = {};
        this.starting(function () {
            // Register the hub proxies as subscribed
            // (instance, shouldSubscribe)
            registerHubProxies(proxies, true);

            this._registerSubscribedHubs();
        }).disconnected(function () {
            // Unsubscribe all hub proxies when we "disconnect".  This is to ensure that we do not re-add functional call backs.
            // (instance, shouldSubscribe)
            registerHubProxies(proxies, false);
        });



        return proxies;
    };

    signalR.hub = $.hubConnection("/signalr", { useDefaultPath: false });
    $.extend(signalR, signalR.hub.createHubProxies());

}(window.jQuery, window));

【问题讨论】:

  • 您的代码看起来不错,我尝试了几乎 100% 的相同,它对我有用。您的 Hub 类是否与 SignalR 服务器位于同一进程/应用程序域中?
  • 我想我在一分钟前就想通了。将 Hub 类移出页面类解决了这个问题。现在我考虑一下这很有意义:\ 所以我把它移到了我的 App_Code 文件夹中的一个新类中,现在我的代理类正在生成。我猜如果集线器是其他东西的子类,集线器代理将不完整。
  • 当我尝试你的代码时,我还在页面中放置了一个集线器类。我同意奇怪,当我尝试它时我不认为它会起作用,但它确实这样做了,肯定是其他东西促成了它。尽管如此很高兴它已修复:)
  • 我希望嵌套集线器只要完全公开就可以工作。
  • 来自 API ref,“SignalR 每次需要处理 Hub 操作(例如客户端连接、断开连接或对服务器进行方法调用时)都会创建 Hub 类的新实例”如果我的集线器类嵌套在另一个类中,则无法做到这一点,对吗?但如果你让它工作......嗯。最后,将它从 Page 中剥离出来迫使我以这样一种方式重构它,以使它作为一个外部类更可重用,因此最终一切都变得更好。我还是想知道这是否是我的问题......

标签: asp.net signalr


【解决方案1】:

1) 确保在最新的信号器 js 文件 jquery.signalR-2.0.2.min.js 之前调用部分:@Scripts.Render("~/bundles/jquery")

2) 如果您使用布局页面,请将 @Scripts.Render("~/bundles/jquery") 移动到标题部分。

3) 还要确保您引用的是最新版本的 signal-r - 在撰写本文时:jquery.signalR-2.0.2.min.js

【讨论】:

    【解决方案2】:

    问题是我的 Hub 类嵌套在我的 Page 类中。把它搬出来解决了这个问题。请参阅我原来的问题的 cmets。

    【讨论】:

      【解决方案3】:

      我最终所做的只是在浏览器中拉出那个动态脚本

      http://my 应用基础 url/signalr/hubs

      然后当来源出现时,我经历了这些步骤

      1. CTRL+A
      2. CTRL+C
      3. 创建文件 hubs.js
      4. CTRL+V
      5. 用 /hubs.js 替换 /signalr/hubs

      可能很野蛮而且过于简单。但是,它对我有用

      【讨论】:

        【解决方案4】:

        signalR R 使用大写字母。

        例如:

        <script src="@Href("~/Scripts/jquery.signalR.js")" type="text/javascript"></script>
        

        代替:

        <script src="@Href("~/Scripts/jquery.signalr.js")" type="text/javascript"></script>
        

        【讨论】:

          猜你喜欢
          • 2012-07-04
          • 1970-01-01
          • 2013-10-14
          • 1970-01-01
          • 1970-01-01
          • 2014-01-14
          • 1970-01-01
          • 2022-08-09
          • 2017-03-11
          相关资源
          最近更新 更多