I have the same situation. You might want to add another mock abstraction if you are trying to call it from several places.
Here is a preview of what I came up with (typescript).
I will start by using. SignalRManager is my "manager" class that abstracts my debuggingHub hub. I have a client method fooChanged that runs on the server.
Somewhere in a module using SignalR, I just call the start method, which does not restart if it is already running.
// ensure signalR is started SignalRManager.start().done(() => { $.connection.debuggingHub.server.init(); });
Your โmoduleโ simply registers a callback through the manager class and whenever your SignalR client method is called, your handler is called.
// handler for foo changed SignalRManager.onFooChanged((guid: string) => { if (this.currentSession().guid == guid) { alert('changed'); } });
This is a simple version of SignalRManager that uses jQuery $.Callbacks to send a query to as many modules as you have. Of course, you can use any mechanism that you need, but this is perhaps the easiest.
module RR { export class SignalRManager { // the original promise returned when calling hub.Start static _start: JQueryPromise<any>; private static _fooChangedCallback = $.Callbacks(); // add callback for 'fooChanged' callback static onfooChanged(callback: (guid: string) => any) { SignalRManager._fooChangedCallback.add(callback); } static start(): JQueryPromise<any> { if (!SignalRManager._start) { // callback for fooChanged $.connection.debuggingHub.client.fooChanged = (guid: string) => { console.log('foo Changed ' + guid); SignalRManager._fooChangedCallback.fire.apply(arguments); }; // start hub and save the promise returned SignalRManager._start = $.connection.hub.start().done(() => { console.log('Signal R initialized'); }); } return SignalRManager._start; } } }
Note. Additional work may be required to fix outages or lost connections.
Simon_Weaver
source share