Permalink
Browse files

added arbitrary site checking

  • Loading branch information...
1 parent 80ade1e commit 48903dea2835de71ffc543a763bef973d6797b3c @coolaj86 committed Apr 11, 2011
Showing with 56 additions and 33 deletions.
  1. +34 −19 online-status/README.markdown
  2. +15 −2 online-status/amionline-test.js
  3. +7 −12 online-status/amionline.js
@@ -11,35 +11,41 @@ Am I Online?
`amionline` can accurately provide your application with the **online status** of your user's browser.
var amionline = require('amionline').create();
- amionline.start('internet', 10000); // check internet connectivity every 10s
- amionline.start('origin', 5000); // check origin connectivity every 5s
+ amionline.start('internet', 4000);
+ amionline.start('origin', 4000);
+ // Accurate
console.log('Initial state is: ' + amionline.status);
- amionline.on('online', function () {
- console.log('Connection State is now online');
+ amionline.on('online', function (err) {
+ console.log('ONLINE');
+ document.getElementById('online-status').innerHTML = 'online';
});
- amionline.on('offline', function () {
- console.log('Connection State is now offline');
+ amionline.on('offline', function (err) {
+ console.log('OFFLINE');
+ document.getElementById('online-status').innerHTML = 'offline';
});
`amionline` can also provide precise information as to the **type of connectivity**.
- amionline.on('browser', function (status) {
+ amionline.on('browser', function (err, status) {
// status === amionline.browserStatus;
- console.log('The browser now believes that it is: ' + status);
+ console.log('BROWSER: ' + status);
+ document.getElementById('browser-status').innerHTML = status;
});
- amionline.on('origin', function (status) {
+ amionline.on('origin', function (err, status) {
// status === amionline.originStatus;
- console.log('In relation to the site origin we are: ' + status);
+ console.log('ORIGIN: ' + status);
+ document.getElementById('origin-status').innerHTML = status;
});
- amionline.on('internet', function (status) {
+ amionline.on('internet', function (err, status) {
// status === amionline.internetStatus;
- console.log('In relation to the internet we are: ' + status);
+ console.log('INTERNET: ' + status);
+ document.getElementById('internet-status').innerHTML = status;
});
@@ -54,30 +60,39 @@ Am I Online?
Example:
- amionline.on('internet', function (status) {
+ amionline.on('internet', function (err, status) {
if ('online' === status && 'offline' === amionline.originStatus) {
console.log('Oopsies! Our own site may be experiencing technical difficulties');
}
});
+`amionline` is convenient
+
+In the case that the current status isn't current enough you can check the status immediately:
+
+ amionline.checkNow('internet', function (err, status) {
+ console.log('checkNow', 'internet', err, status);
+ });
+ amionline.checkNow('origin', function (err, status) {
+ console.log('checkNow', 'origin', err, status);
+ });
`amionline` can be used to register your own connectivity checks
(maybe you have a CORS/XHR2 peer device or a partner site)
- // TODO this is not yet implemented
-
function checkPartnerX(callback) {
$.get('http://our.partnerx.com/status' + new Date().valueOf(), function (data) {
if ('OK' === data) {
- callback('online');
+ callback(null, 'online');
} else {
- callback('offline');
+ callback(null, 'offline');
}
});
+ // might register an error callback
}
amionline.use('partnerx', checkPartnerX, 10000); // run checkPartnerX every 10 seconds
- amionline.on('partnerx', function (status) {
- console.log('');
+ amionline.on('partnerx', function (err, status) {
+ console.log('partnerx', err, status);
});
Events
@@ -3,7 +3,9 @@
require('require-kiss');
- var amionline = require('amionline').create();
+ var amionline = require('amionline').create()
+ , origin = require('amionline-origin');
+
amionline.start('internet', 4000);
amionline.start('origin', 4000);
@@ -66,7 +68,18 @@
amionline.checkNow('origin');
- setTimeout(function () {
+ // Check an arbitrary port, resource, or even a CORS / XHR2 partner device or site
+ function checkPartnerX(callback) {
+ origin.create(callback, { url: "/doesntexist" });
+ }
+ amionline.use('partnerx', checkPartnerX, 2000); // run checkPartnerX every 10 seconds
+ amionline.on('partnerx', function (err, status) {
+ console.log('partnerx', err, status);
+ });
+ amionline.start('partnerx');
+
+
+setTimeout(function () {
amionline.stop('internet');
amionline.stop('origin');
console.log('stopped checks');
View
@@ -7,14 +7,6 @@
, navigator = window.navigator
, Periodic = require('periodic');
- function dummyStrategy() {
- return {
- create: function () {
- return function () {};
- }
- };
- }
-
function create(options) {
options = options || {};
@@ -36,8 +28,8 @@
}
emitter.browserStatus = emitter.status;
- strategies.origin = require('amionline-origin');
- strategies.internet = require('amionline-favinet');
+ strategies.origin = require('amionline-origin').create;
+ strategies.internet = require('amionline-favinet').create;
function createPeriodicEmitter(key, Strategy, howoften) {
var status
@@ -52,13 +44,16 @@
}
function check() {
- Strategy.create(relay);
+ Strategy(relay);
}
periodic = Periodic.create(check, howoften);
periodic.checkNow = function (a, b, c) {
if ('function' === typeof a) {
- Strategy.create(a, b, c);
+ Strategy(function (x, y, z) {
+ a(x, y, z);
+ relay(x, y, z);
+ }, b, c);
} else {
periodic.run();
}

0 comments on commit 48903de

Please sign in to comment.