【问题标题】:SignalR hubs not loading in Edge for extensionSignalR 集线器未在 Edge 中加载以进行扩展
【发布时间】:2018-11-04 03:36:47
【问题描述】:

为了澄清,这个问题是关于基于 Chromium 之前的 Edge 版本。 我正在尝试将 Chrome 扩展程序转换为 Edge 扩展程序,并使用 Microsoft Edge Extension Toolkit 进行了此操作。该扩展连接到在简单的 Windows 窗体应用程序中运行的本地 SignalR 集线器,以将消息从 Web 应用程序来回传递到外部连接的设备。尝试连接时,我从 SignalR 收到此错误:

错误:SignalR:加载集线器时出错。确保您的集线器参考正确,例如脚本 src='/signalr/js'>/script>.

我发现它位于 SignalR jquery 文件中,并且上面有一条注释说当正确引用集线器时错误消息将被替换。我可以导航到 localhost:9562/signalr/hubs 并从集线器查看以下代码。

 /*!
 * ASP.NET SignalR JavaScript Library v2.2.2
 * 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;
                        }

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

        proxies['signalRHub'] = this.createHubProxy('signalRHub'); 
        proxies['signalRHub'].client = { };
        proxies['signalRHub'].server = {
            register: function () {
                return proxies['signalRHub'].invoke.apply(proxies['signalRHub'], $.merge(["Register"], $.makeArray(arguments)));
             },

            setHiCalMode: function (mode) {
                return proxies['signalRHub'].invoke.apply(proxies['signalRHub'], $.merge(["SetHiCalMode"], $.makeArray(arguments)));
             }
        };

        return proxies;
    };

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

}(window.jQuery, window));

我根据文档引用它,转换后的扩展仍然可以在 Chrome 中运行。

这是我在扩展程序中设置连接的方式:

$.connection.hub.url = 'http://localhost:9562/signalr';
$.connection.hub.start().done(init);
signalrHubProxy = $.connection.signalRHub;

【问题讨论】:

    标签: google-chrome-extension signalr microsoft-edge microsoft-edge-extension


    【解决方案1】:

    我猜您尝试移植的扩展正在使用 NativeMessaging。如果是,根据 Edge 的扩展策略,扩展将无法直接与 Win32 应用程序通信。您需要将 Win32 应用程序转换为桌面新娘,同时将无头 UWP 应用程序作为中间件来从 Extension 和 Win32 应用程序传递消息。

    您可以在这里进一步阅读:

    https://docs.microsoft.com/en-us/microsoft-edge/extensions/guides/native-messaging#adding-a-desktop-bridge-component

    【讨论】:

      猜你喜欢
      • 2012-07-23
      • 1970-01-01
      • 1970-01-01
      • 2011-04-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-14
      • 1970-01-01
      相关资源
      最近更新 更多