I don’t know how to explain it but I made a socket.io back-end server on glitch.com and I implemented it in my PlayCanvas project but when 2 or more players join, it starts bugging for some reason. Could you please tell me what I did wrong and how to solve it?
Project Link: PlayCanvas | HTML5 Game Engine
1 Like
Can you share with us your server code? That would make debugging a lot easier.
1 Like
Sure. Here is the server code:
var server = require('http').createServer();
var options = {
cors: true
}
var io = require('socket.io')(server, options);
var players = {};
function Player (id) {
this.id = id;
this.x = 0;
this.y = 0;
this.z = 0;
this.entity = null;
}
io.sockets.on('connection', function(socket) {
socket.on ('initialize', function () {
var id = socket.id;
var newPlayer = new Player (id);
// Creates a new player object with a unique ID number.
players[id] = newPlayer;
// Adds the newly created player to the array.
socket.emit ('playerData', {id: id, players: players});
// Sends the connecting client his unique ID, and data about the other players already connected.
socket.broadcast.emit ('playerJoined', newPlayer);
// Sends everyone except the connecting player data about the new player.
});
socket.on ('positionUpdate', function (data) {
players[data.id].x = data.x;
players[data.id].y = data.y;
players[data.id].z = data.z;
socket.broadcast.emit ('playerMoved', data);
});
});
console.log ('Server started.');
server.listen(3000);
I actually fixed it but all that is left that I want help with is how to disconnect other players when fallen off the edge and how to share the “Box” entity’s health to other players. By the way, for more context about the box entity, if you hit the “Box” entity, its health decreases by 1 and its max health is 10.