javascript - SignalR: Wait for connection to be re-established before invoking methods -
i have hubproxy many client triggered methods described below:
proxy.invoke('hub_subscribe'); proxy.invoke('triggeronserver'); proxy.invoke('datatoserver',somemodel);
now if signalr not connected server , try invoke of above methods, give me connection must started before data can sent.
or connection disconnected before invocation result received.
error.
i aware 1 can utilize connection.statechanged
confirm whether signalr connected or not , invoke methods accordingly. there need log these events can invoked once signalr connection up.
so, there simple way log these methods in case connection disconnected? , later once signalr connection , running, invoke methods?
something proxy.invoke('datatoserver',somemodel).waitforsignalrtobeconnected();
note: continuously connect server after client gets disconnected using this
what need promises using $q
service.
i'm assuming have initialize hub connection first, invoke 3 other methods in order.
create service if dont have 1 managing communication signalr server. inject $q
service , signalr in it.
wrap each of following commands in seperate methods:
function doinitialize() { ... // connect signalr return $q.when(connection.hub.start()); }; function dohubsubscribe() { return $q.when(proxy.invoke('hub_subscribe')); }; function dotriggeronserver() { return $q.when(proxy.invoke('triggeronserver')); }; function dodatatoserver() { return $q.when(proxy.invoke('datatoserver',somemodel)); };
now controller can make nested calls service's methods in way first 1 needs pass in order next ones run:
function initsignalr() { return service.doinitialize().then(function () { return service.dohubsubscribe(); }).then(function () { return service.dotriggeronserver(); }).then(function () { return service.dodatatoserver(); }) };
edit:
if want add more handling invokations, can use jquery .done()
, .fail()
methods this:
function senddatatoserver() { $q.when(proxy.invoke('datatoserver', somemodel)) .done(function (result) { deferred.resolve(result); console.log('success!'); }) .fail(function () { deferred.reject(); console.error('error invoking server!'); })); return deferred.promise; };
you can add more handling code .fail()
method, example add $timeout
service set retries.
hope helps.
Comments
Post a Comment