客户端功能不调用

时间:2019-03-21 05:23:35

标签: asp.net-mvc asp.net-mvc-5 signalr signalr-hub signalr.client

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

此问题与-https://github.com/SignalR/SignalR/issues/4339

直接相关

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

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

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

并根据正确答案进行修复:

  

在$ .connection.hub.start之前的初始化中,调用_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的引用将发生此错误   未删除。此包已弃用;参考必须是   删除,并且必须是SelfHost软件包的1.x版本   已卸载。   (https://docs.microsoft.com/en-us/aspnet/signalr/overview/releases/upgrading-signalr-1x-projects-to-20

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

1 个答案:

答案 0 :(得分:0)

我只是从另一个地方(关于我们的页面)完全调用此函数。只添加相关的脚本和功能。 从那个地方开始,就行了。

在那之后,我更改了一些脚本放置并修复了此问题。

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