JIntro
JIntro

Reputation: 676

SignalR hubs not loading in Edge for extension

For clarification, this question is about the pre-Chromium based version of Edge. I am trying to convert a Chrome extension to an Edge extension and did so using the Microsoft Edge Extension Toolkit. The extension connects to a local SignalR hub running in a simple Windows Form app to pass messages back and forth from the web app to externally connected devices. I am getting this error from SignalR when trying to connect:

Error: SignalR: Error loading hubs. Ensure your hubs reference is correct, e.g. script src='/signalr/js'>/script>.

I found that this is located in the SignalR jquery file and there is a comment above it that says the error message will be replaced when hubs is referenced correctly. I can navigate to localhost:9562/signalr/hubs and see the following code from the hub.

 /*!
 * 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));

I am referencing it according to the documentation and the converted extension still works in Chrome.

Here is how I am setting up my connection in the extension:

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

Upvotes: 0

Views: 697

Answers (1)

WarPro
WarPro

Reputation: 350

I am guessing that the Extension you're trying to port is using NativeMessaging. If yes, as per Edge's Extension policy an Extension wont be able to communiate with a Win32 App directly. You shall need to convert the Win32 App into a desktop bride and at the same time have a headerless UWP App as a middleware to communicate messages from Extension and Win32 App.

You can read about this further here:

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

Upvotes: 1

Related Questions