SignalR and RequireJS : client methods undefined

2019-08-28 17:24发布

问题:

I am trying to use SignalR (1.3) with RequireJS.

My requireJs shim contains :

    "signalr.core": {
            deps: ["jquery"],
            exports: "$.connection"
        },
        "signalr.hubs": {
            deps: ["signalr.core"],
            exports: "$.connection"
        },

I have a module :

define(["jquery", "signalr.hubs"], function ($) {
    var Test = {};
    Test.Init = function () {
        //Code init
        $.connection.myHub.client.ClientMethod = function () { console.log("Hello"); };
    };
    Test.Start = function () {
        return $.connection.hubs.start();
    };
    return Test;
});

I call my module two times in my code, first time for init, second for start :

require(["jquery", "myModule"], function ($, Test) {
    Test.Init();
});
require(["jquery", "myModule"], function ($, Test) {
    Test.Start().done(function () {
        $(document.body).append($("<p />").text("Connected"));
    });
});

But when I call the Test.Start() method, the value of $.connection.myHub.client.ClientMethod is undefined.

What is the correct way to declare my shim to avoid loosing my hubs' client methods ?

Thanks.