Jump to content

I need help with Phaser + socket.io


bousing
 Share

Recommended Posts

Hello my gamedevelopers friends

     I write this post because I have a issue, but I don't know how to solve it.

     I developing a multiplayer HTML5 game with Phaser. Well, is not entirely multiplayer because the players doesn't play among them but they interact with a datebase.

   The game consists in find riddles around a map with the mouse or touches. When the user find the riddle and write the correct answer the backend must be send to client a update of score. That I can't find because my solution doesn't work :(  

   I work with socket.io and NodeJs because I need some connection between the client and the server. I tried the following:

 


//SERVER

   socket.on("connection",function(socket){
    
    socket.emit("websocket",dateJSON);
    
    //dateJSON is a date brought from the datebase, for example the username

    console.log("The server send a message to client");
   });

 
//CLIENT

var UserName;

var socket = io.connect("http://localhost:8000");

socket.on("websocket",function(date){

   UserName = date[0].username; //Like that because I need the username from the JSON
    
});
 
console.log(UserName);

 

If I execute the ultimate instrucction console.log(UserName), I will give get "Undefined"... I don't know why

That was a example of what I want do. I need set a variables of client with date sent from the server. Before that, I will display that values in my game. The update of scores when the player win a riddle I want do the same way, but I need find the solutions. I have a week in this problem.

  If you know the solution or another way to solve it. Please help me :'( the game will be very fun and that is all I need to finish. 

 Greetings!!!

 

Link to comment
Share on other sites

It may be an asynchronous action, so maybe something like this will work.

 

//SERVER

   socket.on("connection",function(socket){
    
    socket.emit("websocket",dateJSON);
    
    //dateJSON is a date brought from the datebase, for example the username

    console.log("The server send a message to client");
   });

 
//CLIENT

var UserName;

var socket = io.connect("http://localhost:8000");

socket.on("websocket",function(date){

   UserName = date[0].username; //Like that because I need the username from the JSON
   log();
});

function log () {
    console.log(UserName);
};
 

 

Link to comment
Share on other sites

Hi Yora, thank you so much for your answer.

I tried, but is doesn't work. I use in this example console.log for show you my results, but just I want set the Username or score, for example. OK. that solution is failure. How I can to solve my objetive ? I mean set a variables in the game with information recive from the server. 

I tried something like that https://github.com/robhawkes/mozilla-festival/blob/master/Player.js  

In that example, the Player class was exported and included in the server script. I tried that but when my browser executed the game, it get a error "exports is not defined. :( 

Greetings again 

Link to comment
Share on other sites

2 hours ago, rgk said:

We don't see your server logic, maybe your not sending the right data back. I would just add the console.log to the socket.on too.

rgk, thank you so much for your answer.

for example this:

 

app.post("/log",function(req,res){
    
    //CONNECTION WITH POSTGRES DB
    pg.connect(connect,function(err,client,done){
        
        
        if(err){
            console.log("Error de conexion",err);
        }
        
        //QUERY TO CHECK THE LOGIN
        
        client.query("SELECT COUNT(*) FROM usuarios WHERE username = $1 and password = $2",[req.body.username,req.body.password],function(err,result){
            
            if(err){
                console.log("No se encontro el usuario",err);
            }
            
            
            var NumRows = result.rows[0].count;
            console.log(NumRows);
            
            if(NumRows == 1){
                
                res.render("index");
                
            }
            else{
                res.render("menu");
                done();
            }
        });
        
        //QUERY FOR TAKE THE USERNAME AND DISPLAY IN THE GAME

        client.query("SELECT username FROM usuarios WHERE username = $1 and password = $2",[req.body.username,req.body.password],function(err,result){
            
            if(err){
                console.log("No se encontro el usuario");
            }
            
            sendUsername(result.rows);
            done();
        
        });
    
    });
    
});

function sendUsername(data){
    
    io.sockets.on("connection",function(socket){
        console.log("CONECTANDO AL CLIENTE CON LA FUNCION sendUsername");
        //socket.emit("usuario",data);
        juego.setNombreUser(data[0].username);
        console.log("Se ha enviado el usuario ",data[0].username);
    });
}

  That is something of my backend logic. I have my datebase in postgreSQL, and I use pg modules (a module for the use of postgres in nodejs). I did a query for take the username. That result is inside in a JSON  (results.row), before, that result is my statement of the function sendUsername.

  The problem is I can't set the username or score with this way. I need something else but I don't know what. 

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...