Beig
Beig

Reputation: 405

Create 'Objects' new for every request

I am using express to build a little web application. (Getting match information from Steam)

As I don't want my Steam Client to be logged in the whole time, I tied the Steam Login in the response.

I created a simple sample app with express and added a router using

app.use('/user', users);

Now in my user.js file

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();
                    });
                });
}

But now when I open the webpage a second time, the client tells me I am logged off. Why isn't the part executed a second time? (create steam var, login, ...?).

Thank you.

If you need any more information, I can upload the sample app. (I don't know if the creation of the http-server is important for my problem)

Upvotes: 3

Views: 331

Answers (1)

Josh Ferrara
Josh Ferrara

Reputation: 767

I made node-csgo so I might be able to provide some help.

What you seem to be doing is creating a new steam client every time someone requests your web page from express - and then subsequently killing it which really isn't the best way to accomplish your task. I'd recommend trying something like what I've done below.

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));
                });
        }
     });
 });

Upvotes: 2

Related Questions