我使用node.js (0.8.15)编写了一个带有基于 Web 的前端的控制器/通信应用程序。socket.io执行定期数据交换。
我的后端需要每秒与 3 个嵌入式板通信(通过 19200bps 的 rs232)。这些板在看到更长的通信故障时会触发看门狗。
当我启动我的 node.js 应用程序时,一切都很好,通信正常。第一次连接到我的 Web 服务器时,我看到 rs232 通信停止了大约 5-10 秒。
重新加载网页时我没有看到这种效果。使用 http 或 https 没有区别。尝试使用--nolazy或--noopt或--noalways_full_compiler等选项运行节点不会改变任何内容。
以下是我的网络服务器的相关部分:
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);