我该怎么做才能将 ram 保持在合理的水平?
在我启动服务器之前,我有大约 140mb 的可用内存。
16 小时后,我还剩下大约 4mb 的空闲内存。
我在具有 256mb 内存的机架空间云上运行它。
var maxMsgs = 50;
var express = require('express'), sio = require('socket.io'), redis = require('redis'), RedisStore = require('socket.io/lib/stores/redis');
var app = express.createServer(), pub = redis.createClient(), sub = redis.createClient(), client = redis.createClient();
app.configure(function () {
app.use(express.bodyParser());
app.use(express.cookieParser());
app.use(express.static(__dirname + '/public'));
app.use(app.router);
});
app.get('/', function (req, res) {
res.sendfile(__dirname + '/index.html');
});
app.listen(8002, function () {
var addr = app.address();
console.log('app listening on http://' + addr.address + ':' + addr.port);
});
var io = sio.listen(app, {log: false}), nicknames = {}, history = [], user_count = 0, topic = {topic: '', setBy: 'Server'}, ytvid = {vid: '', setBy: 'Server'};
io.enable('browser client minification');
io.enable('browser client etag');
io.enable('browser client gzip');
io.set('store', new RedisStore({redisPub : pub, redisSub : sub, redisClient : client}));
//io.set('resource', 'socket');
io.sockets.on('connection', function(socket) {
socket.on('join', function(cu) {
if(cu.username && cu.username != 'Guest') {
socket.nickname = cu.username;
socket.emit('connected', nicknames, history, topic, ytvid);
nicknames[cu.username] = cu;
socket.broadcast.emit('nicknames', nicknames);
user_count++;
//socket.broadcast.emit('announcement', {msg: socket.nickname + ' connected'});
}
});
socket.on('message', function(msg, cb) {
if(msg.msg && msg.msg != '') {
msg.time = Date.now() / 1000;
history.push(msg);
while(history.length > maxMsgs) history.shift();
cb(true, msg.time);
socket.broadcast.emit('message', msg);
}
});
socket.on('stopic', function(t) {
if(t.topic && t.setBy && t.topic != '') {
topic = t;
io.sockets.emit('topic', t);
} else {
topic = {topic: 'No topic set', setBy: 'Admin'};
io.sockets.emit('topic', topic);
}
});
socket.on('sytvid', function(v) {
if(v.vid && v.setBy && v.vid != '') {
ytvid = v;
io.sockets.emit('ytvid', v);
} else {
ytvid = {vid: false, setBy: 'Admin'};
io.sockets.emit('ytvid', ytvid);
}
});
socket.on('get debug', function() {
socket.emit('debug', {users: nicknames, history: history, user_count: user_count, topic: topic});
});
socket.on('send command', function(c) {
if(c.type == 'empty') history = [];
io.sockets.emit('command', c);
});
socket.on('disconnect', function() {
if(!socket.nickname) return;
if(!nicknames[socket.nickname]) return;
//nicknames[socket.nickname].status = 'offline';
delete nicknames[socket.nickname];
//socket.broadcast.emit('announcement', {msg: socket.nickname + ' disconnected'});
socket.broadcast.emit('nicknames', nicknames);
user_count--;
});
});
function inArray(needle,haystack){for(var key in haystack){if(needle===haystack[key]){return true;}}return false;}
function zeroPad(digits,n){n=n.toString();while(n.length<digits){n='0'+n;}return n;}
function time(time){if(time==null)time=new Date();else if((time instanceof Date)===false)time=new Date(time);return time;}