我正在使用 express 构建一个小型 Web 应用程序。 (从Steam获取匹配信息)
因为我不希望我的 Steam 客户端一直处于登录状态,所以我在响应中绑定(bind)了 Steam 登录。
我用 express 创建了一个简单的示例应用程序并使用添加了一个路由器
app.use('/user', users);
现在在我的 user.js 文件中
router.get('/', function(req, res, next) {
if (req.query.id == null || req.query.id == '') {
console.log('wrong');
} else {
var steam = require('steam'),
fs = require('fs'),
csgo = require('csgo'),
bot = new steam.SteamClient(),
CSGO = new csgo.CSGOClient(bot, false);
//... login and event handlers ...
CSGO.on('ready', function () {
console.log('node-csgo ready.');
CSGO.requestRecentGames(CSGO.ToAccountID(req.query.id));
CSGO.on("matchList", function(list) {
res.end(JSON.stringify(list, null, 2));
CSGO.exit();
});
});
}
但是现在当我第二次打开网页时,客户端告诉我我已注销。为什么该部分不执行第二次? (创建 Steam 变量、登录、...?)。
谢谢。
如果您需要更多信息,我可以上传示例应用程序。 (我不知道创建 http 服务器是否对我的问题很重要)
最佳答案
我制作了 node-csgo,所以我可以提供一些帮助。
你似乎在做的是每次有人从 express 请求你的网页时创建一个新的 Steam 客户端 - 然后随后将其杀死,这确实不是完成任务的最佳方式。我建议尝试像我在下面所做的那样的事情。
var steam = require('steam'),
fs = require('fs'),
csgo = require('csgo'),
bot = new steam.SteamClient(),
CSGO = new csgo.CSGOClient(bot, false);
// Do your login and event handling here
// Once the CSGO client is ready, then allow the web server to take requests.
CSGO.on('ready', function() {
console.log('node-csgo ready.');
router.get('/', function(req, res, next) {
if (req.query.id == null || req.query.id == '') {
console.log('wrong');
} else {
CSGO.requestRecentGames(CSGO.ToAccountID(req.query.id));
CSGO.on("matchList", function(list) {
res.end(JSON.stringify(list, null, 2));
});
}
});
});
关于node.js - 为每个请求创建 'Objects' new,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30055359/