我在这里的用户帮助下编写了以下代码。
但我仍然有很高的CPU使用率问题
我希望能够完善此代码的说明和建议。
此代码的目标是从5个不同的数据库中查询两个表并将结果返回到数组
然后客户端将过滤结果并显示相应的结果。
var app = require('http').createServer();
var io = require('socket.io')(app);
var mysql = require('mysql');
var cluster = require('cluster');
var static=[];
var clients = [];
var dbs=[];
var pool = mysql.createPool({
host : '',
user : '',
password : '@@'
});
if(cluster.isMaster){
cluster.fork();
cluster.fork();
cluster.fork();
}else{
io.sockets.on('connection', function (socket) {
clients.push(socket);
socket.on('disconnect',function() {
clients.splice(clients.indexOf(socket), 1);
});
});
pool.getConnection(function(err, connection){
connection.query('use nodejs');
connection.query("SELECT db_name,domain_name FROM tbl_dblist",function(err, rows, fields){
rows.forEach(function(v1){
dbs.push({'db':v1.db_name,'site':v1.domain_name});
})
})
connection.release();
});
setInterval(
function(){
pool.getConnection(function(err, connection){
dbs.forEach(function(v1){
connection.query('use '+v1.db);
connection.query("select value as jackpot,sum(playnum) as played,sum(amount) as total,count(user_id) as player from tbl_config,tbl_user where header='jackpot_amount' ",function(err,rows){
static[v1.site]={'played':rows[0].played,'total':rows[0].total,'player':rows[0].player,'jackpot':rows[0].jackpot}
});
})
connection.release();
});
clients.forEach(function(cl,ind){
qq=cl.handshake.query.domain;
cl.emit('static',static[qq]);
});
},2500);
console.log('Listen On Port 3001');
app.listen(3001);
}
我也有一个大问题
在浏览器控制台日志中o得到了很多
GET http://XXX.XXX.XXX.XXX:3001/socket.io/?domain=XXX&EIO=3&transport=polling&t=1420913085882-3 net::ERR_EMPTY_RESPONSE