首页 > 解决方案 > 客户端函数不调用

问题描述

升级 signalR 和 .NET 版本后我遇到了很多困难。以前我有 1.XX 版本,现在我有 2.4.0 信号 R 版本。

这个问题与 - https://github.com/SignalR/SignalR/issues/4339直接相关

但升级后信号 R 不起作用。

现在的问题是客户端函数无法调用。

我刚试过这个:Signalr doesn't call client side functions

并根据正确答案修复它:

在 $.connection.hub.start 之前的 init 中调用您的 _subscribe 方法。

后来我对这个问题进行了更深入的研究,并在我的 signalr.js 中添加了 console.log

connection.socket.onmessage = function (event) {
    var data;

    try {
        console.log(event.data);
        data = connection._parseResponse(event.data);
    }
    catch (error) {
        transportLogic.handleParseFailure(connection, event.data, error, onFailed, event);
        console.log("socket error" + event.data);
        return;
    }

    if (data) {
        transportLogic.processMessages(connection, data, onSuccess);
    }
};

每个人加入会议后 -> 会议开始并要求投票(这个地方我们应该称之为 signalR)

从投票询问人的角度来看,我看到这样的控制台日志:

普通用户(投票人控制台日志如下所示:

这是来自 Firefox - 另一个用户:

我认为它已经触发 - 集线器“NotificationHub”上的客户端集线器事件“sendOnlineMeetingVoteRequest”。

它也已经达到了服务器端功能,但问题是它从来没有达到这部分代码:

notificationHub.client.sendOnlineMeetingVoteRequest = function (token, meetingId, meetingVoteId) {
    debugger;
    if (token == '@Model.Organization' && '@Model.MeetingId' == meetingId) {
        ShowMeetingOnlineMeetingVotePopup(meetingId, meetingVoteId);
    }
};

我通过http://localhost:33852/signalr/hubs

/*!
 * ASP.NET SignalR JavaScript Library v2.3.0-rtm
 * http://signalr.net/
 *
 * Copyright (c) .NET Foundation. All rights reserved.
 * Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
 *
 */

/// <reference path="..\..\SignalR.Client.JS\Scripts\jquery-1.6.4.js" />
/// <reference path="jquery.signalR.js" />
(function ($, window, undefined) {
    /// <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/js.");
    }

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

                        // Use the actual user-provided callback as the "identity" value for the registration.
                        subscriptionMethod.call(hub, memberKey, makeProxyCallback(hub, memberValue), 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);
        });

        proxies['NotificationHub'] = this.createHubProxy('NotificationHub'); 
        proxies['NotificationHub'].client = { };
        proxies['NotificationHub'].server = {

            sendMeetingStartMessage: function (token, meetingId) {
                return proxies['NotificationHub'].invoke.apply(proxies['NotificationHub'], $.merge(["sendMeetingStartMessage"], $.makeArray(arguments)));
             },

            sendMeetingStopMessage: function (token, meetingId) {
                return proxies['NotificationHub'].invoke.apply(proxies['NotificationHub'], $.merge(["sendMeetingStopMessage"], $.makeArray(arguments)));
             },

            sendMeetingTreeRefreshRequest: function (token, meetingId) {
                return proxies['NotificationHub'].invoke.apply(proxies['NotificationHub'], $.merge(["sendMeetingTreeRefreshRequest"], $.makeArray(arguments)));
             },

            sendMessage: function (token, meetingId, agendaGroupItemId, motionId) {
                return proxies['NotificationHub'].invoke.apply(proxies['NotificationHub'], $.merge(["sendMessage"], $.makeArray(arguments)));
             },

            sendOnlineMeetingVoteCloseRequest: function (token, meetingId, meetingVoteId) {
                return proxies['NotificationHub'].invoke.apply(proxies['NotificationHub'], $.merge(["sendOnlineMeetingVoteCloseRequest"], $.makeArray(arguments)));
             },

            sendOnlineMeetingVoteRequest: function (token, meetingId, meetingVoteId) {
                return proxies['NotificationHub'].invoke.apply(proxies['NotificationHub'], $.merge(["sendOnlineMeetingVoteRequest"], $.makeArray(arguments)));
             },

            sendOnlineVoteCloseRequest: function (token, meetingId, agendaGroupItemId, motionId) {
                return proxies['NotificationHub'].invoke.apply(proxies['NotificationHub'], $.merge(["sendOnlineVoteCloseRequest"], $.makeArray(arguments)));
             },


            sendOnlineVoteRequest: function (token, meetingId, agendaGroupItemId, motionId) {
                return proxies['NotificationHub'].invoke.apply(proxies['NotificationHub'], $.merge(["sendOnlineVoteRequest"], $.makeArray(arguments)));
             },


            sendOnlineVoteResult: function (token, meetingId, agendaGroupItemId, motionId, selectedVotingOptionId) {
                return proxies['NotificationHub'].invoke.apply(proxies['NotificationHub'], $.merge(["sendOnlineVoteResult"], $.makeArray(arguments)));
             }
        };

        return proxies;
    };

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

}(window.jQuery, window));

所以我也没有发现任何错误。尽管如此,我仍然无法弄清楚为什么会发生这种情况,但我浏览了使用 signalR 2.0.3.0 版本的示例项目

我去参考并刚刚注意到这个参考 - Microsoft.AspNet.SignalR.Owin 不包含在我下载的那个示例项目中。

我进一步做了一些调查并发现了这一点:

'以下方法或属性之间的调用不明确' 如果未删除对 Microsoft.AspNet.SignalR.Owin 的引用,则会发生此错误。该软件包已弃用;必须删除引用并且必须卸载 1.x 版本的 SelfHost 包。(https://docs.microsoft.com/en-us/aspnet/signalr/overview/releases/upgrading-signalr-1x-projects-to-20

我需要删除它吗?在我的网络配置中,没有这样的代码。

标签: asp.net-mvcasp.net-mvc-5signalrsignalr-hubsignalr.client

解决方案


我只是从另一个地方调用这个函数——关于我们页面。只添加相关的脚本和功能。从那个地方,它奏效了。

之后,我更改了一些脚本放置并解决了这个问题。

主要的是我没有得到任何错误或任何东西。事情看起来一直在工作。但由于一些脚本放置,它不起作用。


推荐阅读