making error message variable?

Asked

Viewed 62 times

1

personal hello I have a servdor and it is giving a simple error but I do not know how to solve being that the objects are apparently normal see the code below.

  var io = require('socket.io').listen(8000);

   var players = {}, count = 0;

io.sockets.on('connection',
    function (socket)
    {   
        var id = count++; //assign number
        var player = players[id] = new Player(id, 10, 10); //create new      player
    player.socket = socket;


    socket.on('chat',
        function (data)
        {
            var message = player.name + ': ' + data.message;
            socket.emit('chat', { message: message});
            socket.broadcast.emit('chat', { message: message});
        });

    socket.on('position',
        function (data)
        {
            player.x = data.playerX;
            player.y = data.playerY;
            player.z = data.z;


            socket.emit('position', {pid : player.id, playerX : player.x, playerY : player.y, z : player.z});
            socket.broadcast.emit('position', {pid : player.id, playerX : player.x, playerY : player.y, z : player.z});
        }); 

    socket.on('register',
        function (name)
        {
            player.name = name;
            player.registered = true;
            socket.emit('chat', { message : "Server: bem vindo ao server " + player.name + '!'});
            socket.broadcast.emit('chat', { message : "Server: bem vindo ao server " + player.name + '!'});

            for(var p in players)
            {
                //send initial update
                if(!players[p].disconnected)
                { 
                    socket.emit('addPlayer', {pid: players[p].id, x: players[p].x, y: players[p].y, name: players[p].name, sessionid: socket.id});
                    if(players[p].id != player.id)
                        players[p].socket.emit('addPlayer', {pid: player.id, x: player.x, y: player.y, name: player.name})
                }
            }

        });

    socket.on('disconnect',
        function (socket)
        {
            player.disconnected = true;
            delete players[player.id];
            player = null;
        });

});



 function Player(id, x, y, z) {
    this.id = id;
   this.x = x;
   this.y = y;
   this.z = z;
    this.name = "";
    this.disconnected = false;
    this.registered = false;
    this.socket = null;
 }

connects the normal server but when it will receive the data the message of

this.z = z;

"this. z = z is not defined"

since thanks, remembering that it is a server tcp ip socket.io nodejs.

1 answer

1


If you didn’t pass, he doesn’t think:

function Player(id, x, y) {

Where is the z?

  • now giving error z is not defined.

  • 1

    @adailtonmoraes Player(id, 10, 10); <- 3 arguments, but here’s 4 -> function Player(id, x, y, Z) {... you’re not passing the fourth argument.

  • 2

    If you have 4 arguments in your function, you need 4 arguments when declaring it even if it is empty, you need to declare... js is different from php that if you don’t pass it understands as null. Player(10,10,10,10) or at least Player(10,10,10,"").

  • even so did not work gave error of the same method put so Player(id, 10, 10, 1); Function Player(id, x, y, Z) { not funfou.

  • 1

    if the z q takes the application is minuscule, your declaration z has to be too. Try to update your question with the code as ta now that facilitates.

  • same with z in minuscule gave error. newplayer (id, 10, 10, z) gave error z is not defined

  • 1

    ta, does this.id exist, does this. z exist? something else, in the updated code, you’re still passing the z in the statement of the main function Player(id, x, y, Z)

  • in case the z or number would have the properties equal to the 10.10 or z would be to represent a value that the client of some chat user sends but with a value of 0 to 10 understands?

  • Dude, if you pass Player(id, x, y, Z) the big Z, in your code you need to put this. z = Z (more), case sensitive, you know? z is different from Z, a != A....

  • 1

    @adailtonmoraes it is you who marks as solved/ accepted in the answer.

  • only one last question on the player line. z = date. z; and in data transmission nothing is happening: z : player. z}); is not transmitting anything to me.

  • 1

    It’s... this method I never used... but good luck there with the js

  • I decided, it was on the client.

Show 8 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.