SignalR集线器未在Edge中加载以进行扩展

时间:2018-05-24 20:01:54

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

我正在尝试将Chrome扩展程序转换为Edge扩展程序,并使用Microsoft Edge Extension Toolkit执行此操作。该扩展连接到运行在简单Windows窗体应用程序中的本地SignalR中心,以便将消息从Web应用程序来回传递到外部连接的设备。我在尝试连接时从SignalR收到此错误:

  

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

我发现它位于SignalR jquery文件中,上面有一条注释,表示在正确引用集线器时将替换错误消息。我可以导航到localhost:9562 / signalsr / 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;

1 个答案:

答案 0 :(得分:1)

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

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

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