Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/430.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在express中创建新实例时如何访问userid和username_Javascript_Node.js_Express - Fatal编程技术网

Javascript 在express中创建新实例时如何访问userid和username

Javascript 在express中创建新实例时如何访问userid和username,javascript,node.js,express,Javascript,Node.js,Express,形成这个问题真的很难,因为这是一个困难的问题。我有一个游戏服务器文件在这个文件中,我们有 var game_server = module.exports = { games : {}, game_count:0 }, UUID = require('node-uuid'), verbose = true; //Since we are sharing code with the browser, we //are goi

形成这个问题真的很难,因为这是一个困难的问题。我有一个游戏服务器文件在这个文件中,我们有

     var
    game_server = module.exports = { games : {}, game_count:0 },
    UUID        = require('node-uuid'),
    verbose     = true;

    //Since we are sharing code with the browser, we
    //are going to include some values to handle that.
global.window = global.document = global;

    //Import shared game library code.
require('./game.core.js');

    //A simple wrapper for logging so we can toggle it,
    //and augment it for clarity.
game_server.log = function() {
    if(verbose) console.log.apply(this,arguments);
};

game_server.fake_latency = 0;
game_server.local_time = 0;
game_server._dt = new Date().getTime();
game_server._dte = new Date().getTime();
    //a local queue of messages we delay if faking latency
game_server.messages = [];

setInterval(function(){
    game_server._dt = new Date().getTime() - game_server._dte;
    game_server._dte = new Date().getTime();
    game_server.local_time += game_server._dt/1000.0;
}, 4);

game_server.onMessage = function(client,message) {

    if(this.fake_latency && message.split('.')[0].substr(0,1) == 'i') {

            //store all input message
        game_server.messages.push({client:client, message:message});

        setTimeout(function(){
            if(game_server.messages.length) {
                game_server._onMessage( game_server.messages[0].client, game_server.messages[0].message );
                game_server.messages.splice(0,1);
            }
        }.bind(this), this.fake_latency);

    } else {
        game_server._onMessage(client, message);
    }
};

game_server._onMessage = function(client,message) {

        //Cut the message up into sub components
    var message_parts = message.split('.');
        //The first is always the type of message
    var message_type = message_parts[0];

    var other_client =
        (client.game.player_host.userid == client.userid) ?
            client.game.player_client : client.game.player_host;

    if(message_type == 'i') {
            //Input handler will forward this
        this.onInput(client, message_parts);
    } else if(message_type == 'p') {
        client.send('s.p.' + message_parts[1]);
    } else if(message_type == 'c') {    //Client changed their color!
        if(other_client)
            other_client.send('s.c.' + message_parts[1]);
    } else if(message_type == 'l') {    //A client is asking for lag simulation
        this.fake_latency = parseFloat(message_parts[1]);
    }

}; //game_server.onMessage

game_server.onInput = function(client, parts) {
        //The input commands come in like u-l,
        //so we split them up into separate commands,
        //and then update the players
    var input_commands = parts[1].split('-');
    var input_time = parts[2].replace('-','.');
    var input_seq = parts[3];

        //the client should be in a game, so
        //we can tell that game to handle the input
    if(client && client.game && client.game.gamecore) {
        client.game.gamecore.handle_server_input(client, input_commands, input_time, input_seq);
    }

}; //game_server.onInput

    //Define some required functions
game_server.createGame = function(player) {

        //Create a new game instance
    var thegame = {
            id : UUID(),                //generate a new id for the game
            player_host:player,         //so we know who initiated the game
            player_client:null,         //nobody else joined yet, since its new
            player_count:1              //for simple checking of state
        };

        //Store it in the list of game
    this.games[ thegame.id ] = thegame;

        //Keep track
    this.game_count++;

        //Create a new game core instance, this actually runs the
        //game code like collisions and such.
    thegame.gamecore = new game_core( thegame );
        //Start updating the game loop on the server
    thegame.gamecore.update( new Date().getTime() );

        //tell the player that they are now the host
        //s=server message, h=you are hosting

    player.send('s.h.'+ String(thegame.gamecore.local_time).replace('.','-'));
    console.log('server host at  ' + thegame.gamecore.local_time);
    player.game = thegame;
    player.hosting = true;

    this.log('player ' + player.userid + ' created a game with id ' + player.game.id);

        //return it
    return thegame;
在底部你可以看到我们有一个player.userid这是登录的玩家的id

现在我们有了一个game_core.js,我想把这个值传递给game_core.js,这样我就可以在这个文件中使用userid了

 var frame_time = 60/1000; // run the local game at 16ms/ 60hz
 if('undefined' != typeof(global)) frame_time = 45; //on server we run at 45ms, 22hz

( function () {
var lastTime = 0;
var vendors = [ 'ms', 'moz', 'webkit', 'o' ];

for ( var x = 0; x < vendors.length && !window.requestAnimationFrame; ++ x ) {
    window.requestAnimationFrame = window[ vendors[ x ] + 'RequestAnimationFrame' ];
    window.cancelAnimationFrame = window[ vendors[ x ] + 'CancelAnimationFrame' ] || window[ vendors[ x ] + 'CancelRequestAnimationFrame' ];
}

if ( !window.requestAnimationFrame ) {
    window.requestAnimationFrame = function ( callback, element ) {
        var currTime = Date.now(), timeToCall = Math.max( 0, frame_time - ( currTime - lastTime ) );
        var id = window.setTimeout( function() { callback( currTime + timeToCall ); }, timeToCall );
        lastTime = currTime + timeToCall;
        return id;
    };
}

if ( !window.cancelAnimationFrame ) {
    window.cancelAnimationFrame = function ( id ) { clearTimeout( id ); };
}

}() );

/* The game_core class */

var game_core = function(game_instance){

        //Store the instance, if any
    this.instance = game_instance;
        //Store a flag if we are the server
    this.server = this.instance !== undefined;

        //Used in collision etc.
    this.world = {
        width : 720,
        height : 480
    };

        //We create a player set, passing them
        //the game that is running them, as well
    if(this.server) {

        this.players = {
            self : new game_player(this,this.instance.player_host),
            other : new game_player(this,this.instance.player_client)
        };

       this.players.self.pos = {x:20,y:20};
       this.players.self.username = this.instance.player_host.username;
        console.log(this.players.self.username);

    } else {

        this.players = {
            self : new game_player(this),
            other : new game_player(this)
        };

            //Debugging ghosts, to help visualise things
        this.ghosts = {
                //Our ghost position on the server
            server_pos_self : new game_player(this),
                //The other players server position as we receive it
            server_pos_other : new game_player(this),
                //The other players ghost destination position (the lerp)
            pos_other : new game_player(this)
        };

        this.ghosts.pos_other.state = 'dest_pos';

        this.ghosts.pos_other.info_color = 'rgba(255,255,255,0.1)';

        this.ghosts.server_pos_self.info_color = 'rgba(255,255,255,0.2)';
        this.ghosts.server_pos_other.info_color = 'rgba(255,255,255,0.2)';

        this.ghosts.server_pos_self.state = 'server_pos';
        this.ghosts.server_pos_other.state = 'server_pos';

        this.ghosts.server_pos_self.pos = { x:20, y:20 };
        this.ghosts.pos_other.pos = { x:500, y:200 };
        this.ghosts.server_pos_other.pos = { x:500, y:200 };
    }

        //The speed at which the clients move.
    this.playerspeed = 120;

        //Set up some physics integration values
    this._pdt = 0.0001;                 //The physics update delta time
    this._pdte = new Date().getTime();  //The physics update last delta time
        //A local timer for precision on server and client
    this.local_time = 0.016;            //The local timer
    this._dt = new Date().getTime();    //The local timer delta
    this._dte = new Date().getTime();   //The local timer last frame time
var frame_time=60/1000;//以16ms/60hz的频率运行本地游戏
如果(‘未定义’!=typeof(全局))帧时间=45//在服务器上,我们以45毫秒22赫兹的速度运行
(功能(){
var lastTime=0;
var供应商=['ms','moz','webkit','o'];
对于(var x=0;x
在game.core中有(数据)。在这个数据中有一个用户名。但是现在我想在数据中添加用户名,但我不知道如何添加