I want to monitor in real time if a user's device is online or offline. I basically use the info from this site: http://www.joshmorony.com/monitoring-online-and-offline-states-in-an-ionic-application/
I was "upgrading" the code a bit but I still have problems to receive real time network state changes. In my factory is a function called startWatching()
which works awesome - anytime inside the factory and after I access it the first time.
My question: How can I access the changes of the network state inside my controller in real time? I need to show the user some info every time his device is offline.
My controller:
// ### News ###
var newsCtrl = function ($scope, $http, $ionicLoading, PostService, BookMarkService, ConnectivityMonitor) {
console.log('newsCtrl');
var showNews = false;
ConnectivityMonitor.startWatching().then(function(result) {
// promise
console.log('ConnectivityMonitor result: ', result);
showNews = result;
$scope.showNews = showNews;
console.log('showNews: ', showNews);
if(showNews) {
displayNews();
}
}, function(error) {
console.error(error);
});
function displayNews() {
// do some stuff
}
};
newsCtrl.$inject = ['$scope', '$http', '$ionicLoading', 'PostService', 'BookMarkService', 'ConnectivityMonitor'];
My factory:
// ### ConnectivityMonitor ###
var ConnectivityMonitor = function ($rootScope, $cordovaNetwork, $q) {
console.log('ConnectivityMonitor');
var monitorNow = {
isOnline: isOnline,
isOffline: isOffline,
startWatching: startWatching
};
var deferred = $q.defer();
function isOnline() {
if(ionic.Platform.isWebView()) {
deferred.resolve($cordovaNetwork.isOnline());
} else {
deferred.resolve(navigator.onLine);
}
return deferred.promise;
};
function isOffline() {
if(ionic.Platform.isWebView()) {
deferred.resolve(!$cordovaNetwork.isOnline());
} else {
deferred.resolve(!navigator.onLine);
}
return deferred.promise;
};
function startWatching() {
if(ionic.Platform.isWebView()){
$rootScope.$on('$cordovaNetwork:online', function(event, networkState) {
console.log("went online");
console.log("event: ", event);
console.log("networkState: ", networkState);
deferred.resolve($cordovaNetwork.isOnline());
});
$rootScope.$on('$cordovaNetwork:offline', function(event, networkState) {
console.log("went offline");
console.log("event: ", event);
console.log("networkState: ", networkState);
deferred.resolve(!$cordovaNetwork.isOnline());
});
}
else {
window.addEventListener("online", function(e) {
console.log("went online");
console.log("event: ", e);
deferred.resolve(navigator.onLine);
}, false);
window.addEventListener("offline", function(e) {
console.log("went offline");
console.log("event: ", e);
deferred.resolve(!navigator.onLine);
}, false);
}
return deferred.promise;
};
console.log('isOnline: ', monitorNow.isOnline());
console.log('isOffline: ', monitorNow.isOffline());
console.log('startWatching: ', monitorNow.startWatching());
return monitorNow;
};
ConnectivityMonitor.$inject = ['$rootScope', '$cordovaNetwork', '$q'];