我在5001使用Websocket库ws npm,在端口80通信使用nodejs。
我的问题是在一段时间内文件描述符数量增加导致更多的RAM消耗。
以下是我的代码。
var SOCKETS = [];
//Used for publish URL
var app = https.createServer(options, function(req, response){
log.debug("connection:"+ req.url);
var path = url.parse(req.url,true).pathname;
log.debug('path:'+path);
//response.writeHead(200,{ 'Content-Type': 'text/html' });
switch(path){
case '/push':
client.send(user_message);
response.writeHead(200,{ 'Content-Type': 'text/json' });
response.write(JSON.stringify({ 'userids': valid_users}));
break;
}
// Send HTML headers and message
response.end('');
});
app.listen(config.websocket_port);
var WebSocketServer = require('ws').Server
, wss = new WebSocketServer({ server: app } );
wss.on('connection', function(ws) {
ws.on('message', function(userid) {
ws.userid = userid;
if(SOCKETS[userid]){
delete SOCKETS[userid];
}
SOCKETS[userid] = ws;
log.debug("Userid: " + userid.toString() + " connected");
});
ws.on('close', function(){
log.debug(" Socket with userid closed ", ws.userid);
var userid = ws.userid;
if(SOCKETS[userid] == ws){
SOCKETS[userid] = null;
}
delete SOCKETS[userid];
});
ws.on('error', function(){
log.debug(" Socket error ");
});
});
有人可以帮忙吗?