提交 96c7171f 编写于 作者: C CodFrm

在线人数统计api

上级 1ce3bca1
...@@ -8,7 +8,7 @@ var path = require('path'); ...@@ -8,7 +8,7 @@ var path = require('path');
var fs = require('fs'); var fs = require('fs');
const http = require('http'); const http = require('http');
const https = require('https'); const https = require('https');
const redis = require('./redis'); const redisCli = require('./redis');
var privateKey = fs.readFileSync(path.join(__dirname, './certificate/private.key'), 'utf8'); var privateKey = fs.readFileSync(path.join(__dirname, './certificate/private.key'), 'utf8');
var certificate = fs.readFileSync(path.join(__dirname, './certificate/file.crt'), 'utf8'); var certificate = fs.readFileSync(path.join(__dirname, './certificate/file.crt'), 'utf8');
...@@ -33,6 +33,7 @@ httpsServer.listen(SSLPORT, function () { ...@@ -33,6 +33,7 @@ httpsServer.listen(SSLPORT, function () {
var mooc = new moocModel(); var mooc = new moocModel();
var redis = new redisCli();
app.use(bodyParser.json()); app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ app.use(bodyParser.urlencoded({
...@@ -51,9 +52,10 @@ app.use(express.static(path.join(__dirname, 'static'), { ...@@ -51,9 +52,10 @@ app.use(express.static(path.join(__dirname, 'static'), {
})); }));
//在线人数中间件 //在线人数中间件
app.use(function (req, res) { app.use(function (req, res, next) {
var ip = getClientIp(req); var ip = getClientIp(req);
redis.zadd("cxmooc:online", Date.parse(new Date()), ip); redis.onlineAdd(ip);
next();
}); });
app.get('/', function (req, res) { app.get('/', function (req, res) {
...@@ -150,14 +152,13 @@ function mergeAnswer(source, answer) { ...@@ -150,14 +152,13 @@ function mergeAnswer(source, answer) {
} }
app.get('/update', function (req, res) { app.get('/update', function (req, res) {
var time = Date.parse(new Date()); redis.onlineNum(function (err, data) {
redis.zcount("cxmooc:online", time - (5 * 60 * 1000), time, function (err, res) {
res.send({ res.send({
version: config.version, version: config.version,
url: config.update, url: config.update,
enforce: config.enforce, enforce: config.enforce,
injection: config.injection, injection: config.injection,
onlinenum: res onlinenum: data
}); });
}); });
}) })
......
var redis = require('redis'); var redis = require('redis');
module.exports = function () { module.exports = function () {
var client = redis.createClient({ "host": "127.0.0.1", "port": "6379" }); var client = redis.createClient(6379,"127.0.0.1");
client.on("error", function (err) { client.on("error", function (err) {
console.log("Redis error:" + err); console.log("Redis error:" + err);
}); });
return client; this.onlineAdd = function (ip) {
client.zadd("cxmooc:online", [Date.parse(new Date()), ip]);
}
this.onlineNum = function (call) {
var time = Date.parse(new Date());
client.zcount("cxmooc:online", [time - (5 * 60 * 1000), time], function (err, res) {
call(err, res)
});
}
return this
} }
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册