[SOLVED] Socket.io error

Hello, i have made the first 2 steps of the tutorial and got this error

Hi @ayrin,

Have you included the socket.io library?

And added it first in your scripts loading order?

No i didn’t :stuck_out_tongue: now connected…will go on with settings, there is also a tutorial to use a database to store players data?

Ah, I am not aware of a tutorial on that.

Try searching for generous JavaScript tutorials online on that subject.

Thx @Leonidas i will. Now i have an error on server on positionupdate that should be fired by this function is it right?

Network.prototype.updatePosition = function () {
    if (this.initialized) {
        var pos = this.player.getPosition ();
        this.socket.emit ('positionUpdate', {id: this.id, x: pos.x, y: pos.y, z: pos.z});
    }
};

btw, the network.js must be loaded after the player.js?

Have to add glitch to external scripts?

Hey @ayrin, sorry I am away today, I’ll take a look tomorrow and let you know.

1 Like

Hey @ayrin, did you manage to solve your issue?

Hi @Leonidas not yet, i was able to set up the server and seems players are logged in correctly, but i don’t see them in game, seems there must be an error somewhere.
this is my network.js file

var Network = pc.createScript('network');
Network.id = null;
Network.socket = null;

// initialize code called once per entity
Network.prototype.initialize = function() {
    this.player = this.app.root.findByName ('Player');
    this.other = this.app.root.findByName ('Other');
    
    this.socket = io.connect('https://skillful-amenable-sky.glitch.me');
    this.socket.emit ('initialize');
    var socket = this.socket;

    Network.socket= socket;

    var self = this;
    socket.on ('playerData', function (data) {
        self.initializePlayers (data);
    });

    socket.on ('playerJoined', function (data) {
        self.addPlayer (data);
    });
    
    socket.on ('playerMoved', function (data) {
        self.movePlayer (data);
    });
};

// update code called every frame
Network.prototype.update = function(dt) {
    this.updatePosition ();
};

Network.prototype.initializePlayers = function (data) {
    this.players = data.players;
    this.id = data.id;

    for(var id in this.players){
        if(id != this.id){
            this.players[id].entity = this.createPlayerEntity(this.players[id]);
        }
    }
    

    this.initialized = true;
    console.log('initialized');
};

Network.prototype.createPlayerEntity = function (data) {
    var newPlayer = this.other.clone ();
    // Create a new player entity.

    newPlayer.enabled = true;
    // Enable the newly created player.

    this.other.getParent().addChild (newPlayer);
    // Add the entity to the entity hierarchy.

    if (data)
        newPlayer.rigidbody.teleport (data.x, data.y, data.z);
    // If a location was given, teleport the new entity to the position of the connected player.

    return newPlayer;
    // Return the new entity.
};

Network.prototype.addPlayer = function (data) {
    this.players[data.id] = data;
    this.players[data.id].entity = this.createPlayerEntity(data);
};

Network.prototype.movePlayer = function (data) {
    if (this.initialized)
        this.players[data.id].entity.rigidbody.teleport (data.x, data.y, data.z);
};

Network.prototype.updatePosition = function () {
    if (this.initialized) {
        var pos = this.player.getPosition ();
        this.socket.emit ('positionUpdate', {id: this.id, x: pos.x, y: pos.y, z: pos.z});
    }
};
// swap method called for script hot-reloading
// inherit your script state here
// Network.prototype.swap = function(old) { };

// to learn more about script anatomy, please read:
// http://developer.playcanvas.com/en/user-manual/scripting/

Mmm, that’s a lot of code to debug out of context, in your place what I’d test first is the communication between client/server.

Try putting some breakpoints or console.log() statements in your message handlers and check the messages as they arrive.

    socket.on ('playerData', function (data) {
        self.initializePlayers (data);
    });

    socket.on ('playerJoined', function (data) {
        self.addPlayer (data);
    });
    
    socket.on ('playerMoved', function (data) {
        self.movePlayer (data);
    });

Now it seems to work fine, have to set a bunch of other things :frowning: like other players orientation and animations plays. Guess it will be triky.

1 Like