简体   繁体   中英

Node.js/Socket.io How do I stop simple value changing from the console?

I am creating a game where I keep a few variables such as fallingSpeed, runningSpeed etc. Right now my solution to stop the simple console editing in browser by using setTimeout(...){someFunction(...)}; to send the correct data to the client.

So what happens is that, when a player connects the speed of 1 is sent to the client. After that the server waits for a request which is sent to the it when 'space' has either been pressed or released. However, this causes problem since setTimeout(...){...} only runs once. So the client can just press 'space' once and while in the air, open the console and simply type fallingSpeed = 0; and start hovering around the map.

// use socket.io
var io = require("socket.io").listen(server);
var speedTimer;

io.sockets.on('connection', function(socket) {
    socket.emit('fallingSpeed', {'fallingSpeed': 1});

    // get data for if space is pushed or not
    socket.on('spaceData', function(_spaceData) {
        // if space has been pushed set falling speed to 8
        if (_spaceData.pushed == 1) {
            speedFunction(socket, 8);
        }
        // else fall back to speed 1
        else {
            speedFunction(socket, 1);
        }
    });
});

function speedFunction(socket, speed) {
    speedTimer = setTimeout(function() {
        socket.emit('fallingSpeed', {'fallingSpeed': speed});
    }, 1000/60); // send data every frame.
}

I have tried using setInterval(...){...} but this only creates loops which we all know is not the greatest when multiple users are connected. This was my code using setInterval(...){...}.

// use socket.io
var io = require("socket.io").listen(server);
var speedTimer;

io.sockets.on('connection', function(socket) {
    socket.emit('fallingSpeed', {'fallingSpeed': 1});

    // get data for if space is pushed or not
    socket.on('spaceData', function(_spaceData) {
        // if space has been pushed set falling speed to 8
        if (_spaceData.pushed == 1) {
            clearInterval(speedTimer);
            speedTimer = setInterval(function() {
                socket.emit('fallingSpeed', {'fallingSpeed': 8});
            }, 1000/60); // send data every frame.
        }
        // else fall back to speed 1
        else {
            clearInterval(speedTimer);
            speedTimer = setInterval(function() {
                socket.emit('fallingSpeed', {'fallingSpeed': 1});
            }, 1000/60); // send data every frame.
        }
    });
});

So what I need is somehow to check if the value is the correct value when it changes on the clientside.

If you're writing a multiplayer game, you should never trust the client .

Rethink your architecture. Yes, some information should be given to the client to perform prediction and render the physics, but your server must be the authority on what's going on in the game.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM