2016-04-14 42 views
1

我有一个Node.js游戏服务器,我通过运行nodemon app.js来启动它。现在,每当我编辑一个文件服务器重新启动。我已经实现了saveload的功能,并且我希望每次游戏服务器重新启动(由于文件chages)在重新启动之前要保存游戏,以便我可以在重新启动后的以前状态。Nodemon在每次重启之前执行函数

像这样的东西是什么,我想:

process.on('restart', function(doneCallback) { 
    saveGame(doneCallback); 
    // The save game is async because it is writing toa file 
} 

我已经使用SIGUR2事件尝试,但它从未被触发。这是我试过的,但功能从未被调用。

// Save game before restarting 
process.once('SIGUSR2', function() { 
    console.log('SIGUR2'); 
    game.saveGame(function() { 
     process.kill(process.pid, 'SIGUSR2'); 
    }); 
}); 
+0

你有没有尝试https://github.com/remy/nodemon/blob/master/doc/events.md'nodemon.on('restart',...)'? – migg

+0

@migg不,没有包含'nodemon'包,请看看。 – Cristy

+0

@migg不是,那个事件没有被调用。 – Cristy

回答

0

下面的代码在Unix机器上正常工作。现在,由于您的saveGame是异步的,您必须从回调中调用process.kill。

process.once('SIGUSR2', function() { 
    setTimeout(()=>{ 
     console.log('Shutting Down!'); 
     process.kill(process.pid, 'SIGUSR2'); 
    },3000); 

}); 

所以,你的代码,只要你从game.saveGame()函数中执行回调函数看起来不错。

// Save game before restarting 
process.once('SIGUSR2', function() { 
    console.log('SIGUR2'); 
    game.saveGame(function() { 
     process.kill(process.pid, 'SIGUSR2'); 
    }); 
}); 
+1

这仍然不能回答我的问题,你说的只是我的代码工作正常,但在我的情况下它不是(在Windows上)。 – Cristy

相关问题