几天前我开始在Node中开发。我目前正在开发一种使用Node作为服务器端应用程序的在线游戏。此应用程序只是将.html和.js文件提供给客户端并处理游戏的逻辑。 我正在使用Socket.io与客户端交换数据。当一个新的客户端通过Socket.io连接到服务器时,程序会查找一个房间(包含两个用户的套接字的对象数组和处理逻辑的游戏对象),它有一个空的空间并放置用户(注册插座)。 我已经能够正确地向每个客户端发送精灵的位置,但是我从每个套接字检索数据时遇到问题:如果我尝试将一些数据从客户端发送到服务器,那么该数据只会流过一个两个套接字,无论哪个客户端发送它。
初始化房间数组
for(var i = 0; i < roomsNumber; i++) {
//player1 and player 2 are going to store the sockets
rooms[i] = { player1 : null, player2 : null, game: null};
}
处理连接的Socket.io
var io = require('socket.io')(server);
io.on('connection', function(socket){
console.log("We have a new client: " + socket.id);
insertPlayer(socket);
socket.on('disconnect', function() {
console.log("Client has disconnected: " + socket.id);
removePlayer(socket);
});
});
将客户端添加到会议室
function insertPlayer(socket) { //Check for an empty spot in a room
for(var i = 0; i < rooms.length; i++) {
if(!rooms[i].player1) {
rooms[i].player1 = socket; //Register the socket to the room
checkRooms();
return;
}
if(!rooms[i].player2) {
rooms[i].player2 = socket; //Register the socket to the room
checkRooms();
return;
}
}
}
当房间满了时创建游戏对象(并在玩家离开时破坏它)
function checkRooms() { //Checks if a room is full to initialize the game
for(var i = 0; i < rooms.length; i++) {
if(rooms[i].player1 && rooms[i].player2 && !rooms[i].game) {
rooms[i].game = new Game([rooms[i].player1, rooms[i].player2]); //The constructor passes the sockets
console.log('New game started');
}
if((!rooms[i].player1 || !rooms[i].player2) && rooms[i].game) {
rooms[i].game.endGame();
rooms[i].game = null;
console.log('Game stopped');
}
}
}
在游戏课程中接收数据
constructor(sockets) {
this.sockets = sockets;
this.snakes = [];
var self = this;
for(var i = 0; i < this.sockets.length; i++) {
this.snakes[i] = new Snake(i*100 + 100, 200);
var currentSnake = this.snakes[i];
var currentSocket = this.sockets[i];
currentSocket.on('keyPressed', function(data) {
currentSnake.updateDirection(data); //Calls a Game method that updates the direction of the sprite
console.log(currentSocket.id + " - " + data);
});
}
答案 0 :(得分:1)
我发现currentSnake
构造函数中的Game
变量存在问题。该变量的范围限定在整个构造函数中,因此那里只有一个变量,但是您尝试为每个玩家设置一个单独的变量并且不起作用。因此,每当你收到keyPressed
消息时,无论关键来自哪个玩家,你都可以对同一条蛇进行操作。
如果您正在使用node.js v6 +,那么将该构造函数中的var currentSnake
更改为let currentSnake
,以便将变量分别限定为for
循环的每次调用。
constructor(sockets) {
this.sockets = sockets;
this.snakes = [];
var self = this;
for(var i = 0; i < this.sockets.length; i++) {
this.snakes[i] = new Snake(i*100 + 100, 200);
// change these to be declared with let so they are separately scoped
// for each invocation of the for loop
let currentSnake = this.snakes[i];
let currentSocket = this.sockets[i];
currentSocket.on('keyPressed', function(data) {
currentSnake.updateDirection(data); //Calls a Game method that updates the direction of the sprite
console.log(currentSocket.id + " - " + data);
});
}
}
或者,如果您运行的是旧版本的node.js,请切换到.forEach()
以替换for
循环,因为这也将为循环的每次调用创建一个新的单独范围。 / p>
constructor(sockets) {
this.sockets = sockets;
this.snakes = [];
var self = this;
this.sockets.forEach(function(currentSocket, i) {
this.snakes[i] = new Snake(i*100 + 100, 200);
var currentSnake = this.snakes[i];
currentSocket.on('keyPressed', function(data) {
currentSnake.updateDirection(data); //Calls a Game method that updates the direction of the sprite
console.log(currentSocket.id + " - " + data);
});
});
}