SignalR不生成集线器类

时间:2013-10-09 02:30:50

标签: asp.net signalr

我有一些基本的配置问题试图让我的第一个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));

4 个答案:

答案 0 :(得分:2)

1)确保以下部分:@ Scripts.Render(“〜/ bundles / jquery”)在最新的signalr js文件之前被调用:jquery.signalR-2.0.2.min.js

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

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

答案 1 :(得分:0)

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

答案 2 :(得分:0)

我最终做的就是在浏览器中提取动态脚本

http://my app base url / signalr / hubs

然后当源出现时我完成了这些步骤

  1. CTRL + A
  2. CTRL + C
  3. 创建文件hubs.js
  4. CTRL + V
  5. 用/hubs.js替换/ signalr / hubs
  6. 可能是野蛮而且过于简单。但是,它对我有用

答案 3 :(得分:0)

对signalR R使用大写字母。

例如:

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

而不是:

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