Skip to content

Instantly share code, notes, and snippets.

@philbarresi
Created December 29, 2014 00:10
Show Gist options
  • Select an option

  • Save philbarresi/55918919f495ab92b3f6 to your computer and use it in GitHub Desktop.

Select an option

Save philbarresi/55918919f495ab92b3f6 to your computer and use it in GitHub Desktop.
AngularJS Connection Provider for SignalR
// We start our app
var myApp = angular.module("myApp", []);
myApp.provider('connection', function Connection() {
var connection = $.connection,
reconnectDelay = 1500,
maxReconnectDelay = 60000;
// allows you to set logging on before the connection runs
this.showLogging = function () {
$.connection.hub.logging = true;
};
// used to override the default values
this.setup = function (queryString, delay, maxDelay) {
reconnectDelay = delay || reconnectDelay;
maxReconnectDelay = maxDelay || maxReconnectDelay;
$.connection.hub.qs = queryString;
};
// Used to get the connection to add client callbacks, if so desired, in the config stage
this.getConnection = function () {
return connection;
};
// This is what is returned when we inject 'connection'
this.$get = ['$q', '$timeout',
function connectionService($q, $timeout) {
var self = this,
failedConnectionAttempts = 0,
loadedDefered = $q.defer(),
isLoaded = loadedDefered.promise,
loading = false,
initialized = false;
function whenReady(serverCall) {
return isLoaded.then(function () {
return $q.when(serverCall());
});
}
function init() {
// if we are currently loading, abort
if (loading) {
return;
}
// if we have not yet been initialized (ie: we are reconnecting)
// then we need to setup the disconnection event
if (!initialized) {
initialized = true;
connection.hub.disconnected(function () {
loadedDefered = $q.defer();
isLoaded = loadedDefered.promise;
loading = false;
var newDelay = reconnectDelay * (++failedConnectionAttempts);
$timeout(init, Math.min(Math.max(reconnectDelay, newDelay), maxReconnectDelay));
});
}
// set loading to be true now that we're handling the connection start
loading = true;
connection.hub.start().done(function () {
loading = false;
// resolve the loading defered so that
loadedDefered.resolve();
failedConnectionAttempts = 0;
}).fail(function () {
/// <summary>Panics; figure out what to do here later</summary>
loadedDefered = $q.defer();
isLoaded = loadedDefered.promise;
});
}
init();
// so that we can say `connection.ready().then(function() { return myServerCall(); });`
self.ready = whenReady;
self.hubs = connection;
return self;
}];
return this;
});
// In your main.js file, or wherever you setup the providers
myApp.config(["connectionProvider",
function (connectionProvider) {
if (window.location.hostname === "localhost") connectionProvider.showLogging();
connectionProvider.setup("browser", 1500, 60000, null);
}]);
// An example of moving your client-to-server data into a service
myApp.service('myData', ['connection',
function (connection) {
var self = this,
ready = connection.ready,
myHubServer = connection.hubs.myHub.server;
this.getMyData = function () {
return ready(function () {
return myHubServer.getMyData();
});
};
return this;
}]);
// Which we may then use in the controller
myApp.controller('MyController', ['$scope', 'myData',
function ($scope, myData) {
$scope.someDataArray = [];
myData.getMyData().then(function (allMyThings) {
$scope.someDataArray = allMyThings;
});
}]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment