Node.js / Socket.io如何从控制台停止简单的值更改?

时间:2022-08-22 15:48:14

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.

我正在创建一个游戏,我保留了一些变量,如fallingSpeed,runningSpeed等。现在我的解决方案是使用setTimeout(...){someFunction(...)}停止在浏览器中进行简单的控制台编辑;将正确的数据发送给客户端。

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.

所以会发生的是,当玩家连接时,速度为1会被发送到客户端。之后,服务器等待按下或释放“空间”时发送给它的请求。但是,这会导致问题,因为setTimeout(...){...}只运行一次。所以客户端只需按下“空格”一次,然后在空中打开控制台,只需输入fallingSpeed = 0;并开始在地图上徘徊。

// 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(...){...}.

我尝试过使用setInterval(...){...},但这只会创建一个循环,当连接多个用户时,我们都知道这些循环并不是最好的。这是我使用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.

所以我需要的是以某种方式检查值是否在客户端更改时是否是正确的值。

1 个解决方案

#1


0  

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.

重新思考你的建筑。是的,应该向客户提供一些信息以执行预测并渲染物理,但是您的服务器必须是游戏中发生的事情的权威。

#1


0  

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.

重新思考你的建筑。是的,应该向客户提供一些信息以执行预测并渲染物理,但是您的服务器必须是游戏中发生的事情的权威。