我使用node.js (0.8.15)编写了一个基于web的前端控制器/通信应用程序。socket.io执行定期数据交换。
我的后端需要(通过rs232 19200 via)每秒钟与3块嵌入式板交谈。当他们看到更长时间的沟通失败时,这些委员会就会触发警犬。
当我启动我的node.js应用程序时,一切都很好,通讯就能正常工作。一旦我的web服务器第一次连接到,我就会看到rs232通信停止了大约5-10秒。
在重新加载网页时,我没有看到这种效果。使用http或https没有任何区别。尝试使用--nolazy或--noopt或之类的选项运行节点不会改变任何事情。
以下是我的web服务器的相关部分:
var requests =
[ { method: 'GET', pattern: '/', type: 'text/html', subst: '/index.html' },
{ method: 'GET', pattern: '/index.html', type: 'text/html' },
{ method: 'GET', pattern: '/favicon.ico', type: 'image/x-icon' },
{ method: 'GET', pattern: '/js/[^/]+\.js', type: 'text/javascript' },
{ method: 'GET', pattern: '/css/[^/]+\.css', type: 'text/css' },
{ method: 'GET', pattern: '/img/[^/]+\.jpg', type: 'image/jpeg' },
{ method: 'GET', pattern: '/img/[^/]+.png', type: 'image/png' },
];
function response (req, res)
{
var method = req.method;
var path = Path.normalize (Url.parse (req.url).pathname);
console.log (method+' '+path);
for (i = -1; ++i < requests.length;)
{
var r = requests[i];
if (r.method == method && path.search ('^'+r.pattern+'$') == 0)
{
if ('subst' in r)
{
path = r.subst;
};
if (r.type.match (/^text/))
{
Fs.readFile (__dirname+path, 'utf8', function (err, data)
{
if (err)
{
res.writeHead (500);
res.end ('Error reading '+path);
}
else
{
res.writeHead (200, {'Content-Type': r.type});
res.end (data, 'utf8');
}
});
return;
}
else
{
Fs.readFile (__dirname+path, function (err, data)
{
if (err)
{
res.writeHead (500);
res.end ('Error reading '+path);
}
else
{
res.writeHead (200, {'Content-Type': r.type});
res.end (data);
}
});
return;
}
}
};
console.log ('error 404');
res.writeHead (404);
res.end ();
}
var httpServer = Http.createServer (response);
httpServer.listen (47080);
console.log ('http server running at port 47080');
var basic = Auth ({
authRealm : "controller",
authFile : __dirname + '/users.htpasswd'
});
function httpsResponse (req, res)
{
basic.apply (req, res, function (username)
{
response (req, res);
});
}
var options = {
key: Fs.readFileSync (__dirname + '/controller.pem'),
cert: Fs.readFileSync (__dirname + '/controller.cert')
};
var httpsServer = Https.createServer (options, httpsResponse);
httpsServer.listen(47443);
console.log ('https server running at port 47443');
function connected (socket)
{
console.log ('Listener connected');
theApp.addSocket (socket);
socket.on ('disconnect', function ()
{
theApp.removeSocket (socket);
});
}
var httpListen = Io.listen (httpServer, { 'log level':1 });
var httpsListen = Io.listen (httpsServer, { 'log level':1 });
httpListen.sockets.on ('connection', connected);
httpsListen.sockets.on ('connection', connected);发布于 2013-01-28 15:46:09
我将我的应用程序重写为两个进程:一个用于web服务器,另一个用于串行通信。这改善了一些事情,但却解决了这个问题。
真正的问题似乎是垃圾收集器。
在使用--公开-gc启动节点并定期显式调用global.gc()之后,应用程序运行平稳。
https://stackoverflow.com/questions/14084145
复制相似问题