Created
August 10, 2012 07:42
-
-
Save yuanchuan/3312396 to your computer and use it in GitHub Desktop.
判断QQ在不在线
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*======================================== | |
* 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' ); | |
}); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*======================================== | |
* API | |
*=======================================*/ | |
check( | |
, 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); | |
} | |
}); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*======================================== | |
* 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' | |
); | |
}).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