0

node.js (0.8.15)を使用して、Web ベースのフロントエンドでコントローラー/通信アプリケーションを作成しました。socket.ioは定期的なデータ交換を実行します。

バックエンドは毎秒 3 つの組み込みボードと (19200bps の rs232 経由で) 通信する必要があります。これらのボードは、通信障害が長く続くとウォッチドッグをトリガーします。

node.js アプリを起動すると、問題なく通信できます。初めてWeb サーバーに接続するとすぐに、rs232 通信が約 5 ~ 10 秒間停止することがわかります。

Web ページをリロードしても、この効果は見られません。http または https を使用しても違いはありません。--nolazyまたは--nooptまたは--noalways_full_compilerなどのオプションを使用してノードを実行しようとしても、何も変わりません。

私の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);
4

1 に答える 1

0

アプリケーションを2つのプロセスに書き直しました。1つはWebサーバー用で、もう1つはシリアル通信用です。それは物事を改善しましたが、問題を解決することはできませんでした。

本当の問題はガベージコレクターのようです。

--expose-gcでノードを起動し、定期的にglobal.gc()を明示的に呼び出した後、アプリケーションはスムーズに実行されます。

于 2013-01-28T15:46:09.033 に答える