我的socket.io聊天应用程序有点问题。当我启动时,它使用1%RAM,当它运行2天时,它会上升到10%+并停止工作,我必须一次又一次地重新启动....这是我的代码,我怎么能修理它? 谢谢你的答案!
var https = require('https');
var fs = require('fs');
var mysql = require('mysql');
var mysqlInfo;
mysqlInfo = {
host : 'localhost',
user : 'user',
password : 'pass',
database : 'user',
charset : 'utf8_general_ci'
};
var mysqlConnection = mysql.createConnection(mysqlInfo);
var options = {
key: fs.readFileSync('ssl/server.key'),
cert: fs.readFileSync('ssl/server.crt'),
ca: fs.readFileSync('ssl/ca.crt')
};
var app = https.createServer(options);
io = require('socket.io').listen(app);
app.listen(3000);
var login_users = {};
var channels = ['english'];
io.on('connection', function(socket){
setTimeout(function(){
socket.emit('login', '');
}, 1000);
socket.join('english');
socket.channel='english';
socket.on('login', function(login){
var login_json = JSON.parse(login);
mysqlConnection.query("SELECT id FROM users WHERE id=? and token=?", [login_json.id,login_json.token], function(err, results) {
if(results.length==1){
login_users[socket.id] = socket;
login_users[socket.id]["id"] = login_json.id;
login_users[socket.id]["token"] = login_json.token;
socket.join(login_json.id);
}
});
});
socket.on('chat', function(message){
if(typeof socket.id !== 'undefined'){
mysqlConnection.query("SELECT * FROM users WHERE id=? and token=?", [socket.id,socket.token], function(err, results) {
io.in(socket.channel).emit('chat', '{"message":"'+message+'"}');
}
});
}
});
socket.on('channel_change', function(channel){
if (channels.indexOf(channel) > -1 && socket.channel!=channel) {
socket.leave(socket.channel);
socket.join(channel);
socket.channel=channel;
}
});
});
setInterval(function () {
mysqlConnection.query('SELECT 1');
}, 5000);
答案 0 :(得分:0)
使用它们后需要关闭mysql连接。 基本上发生的是你不断进行这些查询并保持连接对象打开。有可能会定期丢弃连接,但您的节点脚本仍在维护资源以保持连接打开并为后续连接创建新连接。这是内存泄漏的来源。您应始终为每个查询创建新连接,并在获得结果时将其关闭。
var https = require('https');
var fs = require('fs');
var mysql = require('mysql');
var mysqlInfo;
mysqlInfo = {
host : 'localhost',
user : 'user',
password : 'pass',
database : 'user',
charset : 'utf8_general_ci'
};
var options = {
key: fs.readFileSync('ssl/server.key'),
cert: fs.readFileSync('ssl/server.crt'),
ca: fs.readFileSync('ssl/ca.crt')
};
var app = https.createServer(options);
io = require('socket.io').listen(app);
app.listen(3000);
var login_users = {};
var channels = ['english'];
io.on('connection', function(socket){
setTimeout(function(){
socket.emit('login', '');
}, 1000);
socket.join('english');
socket.channel='english';
socket.on('login', function(login){
var login_json = JSON.parse(login);
var mysqlConnection = mysql.createConnection(mysqlInfo);
mysqlConnection.query("SELECT id FROM users WHERE id=? and token=?", [login_json.id,login_json.token], function(err, results) {
if(results.length==1){
login_users[socket.id] = socket;
login_users[socket.id]["id"] = login_json.id;
login_users[socket.id]["token"] = login_json.token;
socket.join(login_json.id);
}
mysqlConnection.end()
});
});
socket.on('chat', function(message){
if(typeof socket.id !== 'undefined'){
var mysqlConnection = mysql.createConnection(mysqlInfo);
mysqlConnection.query("SELECT * FROM users WHERE id=? and token=?", [socket.id,socket.token],
function(err, results) {
mysqlConnection.end();
io.in(socket.channel).emit('chat', '{"message":"'+message+'"}');
}
});
}
});
socket.on('channel_change', function(channel){
if (channels.indexOf(channel) > -1 && socket.channel!=channel) {
socket.leave(socket.channel);
socket.join(channel);
socket.channel=channel;
}
});
});