three.js socket.io multiplayer system

343
August 13, 2017, at 12:33 PM

So i have built a simple scene in three.js and its really cool to play with but i was wanting to add some multiplayer capability running from a socket.io server so i add the player like this

var username = prompt("whats yo name")

and then i do this when all the models are loaded

socket.emit('addPlayer', username)

so then on my server side this happens

socket.on('addPlayer', function(username) {
    players.push(username)
    console.log(username + " joined")
    console.log("online Users " + players)
    socket.broadcast.emit('syncPlayers', players)
    socket.emit('syncPlayers', players)
})

all of that works exatly how i planned

so when syncPlayers is called on all the clients

socket.on('syncPlayers', function(players) {
    players.forEach(function(value) {
        if (value == username) {
            console.log("not adding " + value + " thats you ")
        } else {
            console.log("player Online " + value);
            newplayer = value;
            addPlayer(newplayer)
        }
    });
})

and then of course addPlayer is called passing the var new player

function addPlayer() {
    console.log("adding " + newplayer)
    charObjectName = newplayer + "Char"
    console.log("added" + charObjectName)
    charObjectName = new THREE.Mesh(
        new THREE.BoxGeometry(3, 3, 3),
        new THREE.MeshPhongMaterial({
            color: 0xffffff,
            map: crateTexture,
            bumpMap: crateBumpMap,
            normalMap: crateNormalMap
        })
    );
    scene.add(charObjectName)
    charObjectName.position.set(10, 10, 10)
}

right now i make each new player a cube because it keeps saying undefined or something when i try to load a loaded model but thats for another time

but i thought that my problem could possibly lie in this line

charObjectName = newplayer + "Char"

like maybe it wasn't being set correctly so i console logged it and it is the value i expected

but when i try to update the position of the players cube to there position like this

    if (keyboard[87]) { // W key
        camera.position.x -= Math.sin(camera.rotation.y) * player.speed;
        camera.position.z -= -Math.cos(camera.rotation.y) * player.speed;
        socket.emit('updateXZPos', username, camera.position.x, camera.position.z)
    }
    if (keyboard[83]) { // S key
        camera.position.x += Math.sin(camera.rotation.y) * player.speed;
        camera.position.z += -Math.cos(camera.rotation.y) * player.speed;
        socket.emit('updateXZPos', username, camera.position.x, camera.position.z)
    }
    if (keyboard[65]) { // A key
        camera.position.x += Math.sin(camera.rotation.y + Math.PI / 2) * player.speed;
        camera.position.z += -Math.cos(camera.rotation.y + Math.PI / 2) * player.speed;
        socket.emit('updateXZPos', username, camera.position.x, camera.position.z)
    }
    if (keyboard[68]) { // D key
        camera.position.x += Math.sin(camera.rotation.y - Math.PI / 2) * player.speed;
        camera.position.z += -Math.cos(camera.rotation.y - Math.PI / 2) * player.speed;
        socket.emit('updateXZPos', username, camera.position.x, camera.position.z)
    }

and this is what it does on the server side

socket.on('updateXZPos', function(username, pos1, pos2) {
    console.log(username + ' x-' + pos1 + '-   y-' + pos2)
    socket.broadcast.emit('updateZPos', username, pos1, pos2)
})

and i get the console to log things perfectly

then on the client when i call updateZPos function

socket.on('updateZPos', function(username, pos1, pos2) {
    console.log(pos1 + pos2)
    toMove = username + "Char"
    console.log(toMove)
    toMove.position.x = pos2, toMove.position.z = pos2
})

apparently i cant set x property of undefined so the var toMove isnt correct maybe?

sorry if this is a long drawn out question for something simple but its really bothering me

Rent Charter Buses Company
READ ALSO
WebRTC: Add rooms to simple WebRTC & WebSocket app - Advice Needed

WebRTC: Add rooms to simple WebRTC & WebSocket app - Advice Needed

I am very new to WeRTC and coding, so apologies if this is not a clear question

335
Pm2 keymetrics tool doesn't show same result as htop

Pm2 keymetrics tool doesn't show same result as htop

I'm running my couple of nodejs application (in the same server) with pm2 cluster mode and I'm registered both the application in the keymetricsI'm currently using keymetrics tool as free version so some the process doesn't show in the tool

286
Instructions not executed into the right order with nodejs

Instructions not executed into the right order with nodejs

I am new to nodejs and trying to cat multiple css files on-the-fly while codingThe package chokidar allow me to call a function when a file is modified, however I have a problem with the execution

225