Skip to content

Instantly share code, notes, and snippets.

@yuanchuan
Created August 10, 2012 07:42
Show Gist options
  • Save yuanchuan/3312396 to your computer and use it in GitHub Desktop.
Save yuanchuan/3312396 to your computer and use it in GitHub Desktop.
判断QQ在不在线
/*========================================
* API (普通的方式)
*=======================================*/
check(qq, function(on) {
if (on) {
} else {
}
});
/*========================================
* Implementation
*=======================================*/
var http = require('http');
function check(qq, fn) {
var url = 'http://wpa.qq.com/pa?p=1:' + qq + ':1';
http.get(url, function(res) {
fn( res.headers['content-length'] === '2329' );
});
}
/*========================================
* API
*=======================================*/
check(
qq
, function(qq) {
//on
}
, function(qq) {
//off
}
);
/*========================================
* Implementation
*=======================================*/
var http = require('http');
function check(qq, on, off) {
var url = 'http://wpa.qq.com/pa?p=1:' + qq + ':1';
http.get(url, function(res) {
if ( res.headers['content-length'] === '2329' ) {
on && on(qq);
} else {
off && off(qq);
}
});
}
/*========================================
* API
*=======================================*/
check(qq)
.on(function(qq) {
})
.off(function(qq) {
})
/*========================================
* Implementation
*=======================================*/
var http = require('http')
, events = require('events');
var check = (function() {
function check(qq) {
this.qq = qq;
this.ee = new events.EventEmitter();
return this._check();
};
check.prototype._check = function() {
var url = 'http://wpa.qq.com/pa?p=1:' + this.qq + ':1';
http.get(url, (function(res) {
this.ee.emit(
(res.headers['content-length'] === '2329') ? 'on' : 'off'
, qq
);
}).bind(this));
return this;
};
check.prototype.on = function() {
this.ee.on('on', fn);
return this;
}
check.prototype.off = function(fn) {
this.ee.on('off', fn);
return this;
};
return function(qq) {
return new check(qq);
};
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment