DVLP / signalr-no-jquery

120 stars 77 forks source link

Generated Hub Proxy #44

Closed ganySA closed 4 years ago

ganySA commented 5 years ago

How can i link my generated hub proxy?

Do i need to refactor all the jQuery out?

Thanks

(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['rTD'] = this.createHubProxy('rTD'); 
        proxies['rTD'].client = { };
        proxies['rTD'].server = {
            broadcastDeleteAppointment: function (appointment) {
                return proxies['rTD'].invoke.apply(proxies['rTD'], $.merge(["broadcastDeleteAppointment"], $.makeArray(arguments)));
             },

            broadcastNewAppointment: function (appointment) {
                return proxies['rTD'].invoke.apply(proxies['rTD'], $.merge(["broadcastNewAppointment"], $.makeArray(arguments)));
             },

            broadcastUpdateAppointment: function (appointment) {
                return proxies['rTD'].invoke.apply(proxies['rTD'], $.merge(["broadcastUpdateAppointment"], $.makeArray(arguments)));
             },

            hello: function (appointment) {
                return proxies['rTD'].invoke.apply(proxies['rTD'], $.merge(["Hello"], $.makeArray(arguments)));
             }
        };

        return proxies;
    };

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

}(window.jQuery, window));
dgwaldo commented 5 years ago

Same question here, did you ever figure this out?

ganySA commented 5 years ago

No

On Tue, 04 Jun 2019 at 19:23, Don Waldo notifications@github.com wrote:

Same question here, did you ever figure this out?

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/DVLP/signalr-no-jquery/issues/44?email_source=notifications&email_token=AAYKTVTLGPSS6JMAWL2HXDLPY2QKJA5CNFSM4GDBKC2KYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGODW5I5KI#issuecomment-498765481, or mute the thread https://github.com/notifications/unsubscribe-auth/AAYKTVVEUIXCGCTRRDAKMA3PY2QKJANCNFSM4GDBKC2A .

dgwaldo commented 5 years ago

I think I did, generate your own proxies, don't use the ones on the endpoint.

import { hubConnection } from 'signalr-no-jquery';

const connection = hubConnection('/signalr', {});
const hubProxy = connection.createHubProxy('integrationStatusHub');

type ReportIntegrationStatusCallback = (message: string) => void;

export function connectSignalRIntegrationStatusHub(integrationCallback: ReportIntegrationStatusCallback) {
    // set up event listeners for the hub proxy
    hubProxy.on('reportStatusAsync', function (message) {
        integrationCallback(message);
    });

    connection.start({ jsonp: true })
        .done(function () { console.log('SignalR connected, connection ID=' + connection.id); })
        .fail(function () { console.log('Could not connect'); });
}