Socket.io如何存储房间';s用户?

Socket.io如何存储房间';s用户?,socket.io,Socket.io,我正在使用socket.io和nodejs开发一个游戏。当两个玩家完成他们所拥有的东西时,他们会得到一组盒子,这是一个数组,我会记录下需要多少组盒子 问题是,每当有人提出请求时,我想向房间里的两名球员发送请求计数。我可以在数据库中保存这些信息,但这样做效率很低,这是一个实时多人游戏 因为我必须将两个玩家的盒数都发送到房间,我认为将他们的盒数放在房间数组中会有所帮助,但无法计算。我只是想提出一个想法: 在socket.io文件室中保存的信息如下: { someUniqueSocketId : tr

我正在使用socket.io和nodejs开发一个游戏。当两个玩家完成他们所拥有的东西时,他们会得到一组盒子,这是一个数组,我会记录下需要多少组盒子

问题是,每当有人提出请求时,我想向房间里的两名球员发送请求计数。我可以在数据库中保存这些信息,但这样做效率很低,这是一个实时多人游戏

因为我必须将两个玩家的盒数都发送到房间,我认为将他们的盒数放在房间数组中会有所帮助,但无法计算。我只是想提出一个想法:

在socket.io文件室中保存的信息如下:

{ someUniqueSocketId : true, someAnotherSocketId : true}
我希望它保持这样:

{someUniqueSocketId : {setCountOfThisPlayer : true} ,
 someAnotherSocketId : {setCountOfThatPlayer : true}}
或者也许有更好的方法来保存这样的信息

编辑:

当玩家发送startGame信号时,此操作有效:

function startGame(){
    this.setCount = 0;
    io.to(matchRoom).emit('allSets', allSets);
};

Allset是所有框的数组。它通过这个.setCount将盒数保存为套接字的属性。但我无法访问其他播放器的属性,因此这不起作用。

如果不将盒数存储在数据库中,您可以将其存储在服务器上的变量中。不推荐使用,因为它会吃掉mem并且不会扩展。也许您可以利用每个用户的本地存储并以这种方式进行管理。这需要一些编码,但有几个函数可以处理客户端,您还可以有一个额外的函数来更新连接到套接字的服务器/播放器。

您可能需要为服务器添加更多的结构,以跟踪socket.io功能之外的播放器和房间

下面是一个简单房间设置的快速示例。 我把它保持得非常简单,并且沉溺于一些结构性的捷径,所以它可能不会通过最佳实践审查

var players = {};
var rooms = {};
rooms['randomId236512'] = {// add an example starter room
    id: 'randomId236512',
    name: 'Happy Box House Room',
    players: [],
    started: false,
    setCount: 0
};

function findOpenRoom() {
    //here you would search through the list of games waiting for players
    //if no rooms, make one, add it to the list, then return it
    return rooms[0];
}

io.on('connection', function (socket) {

var newPlayer = {//A better pratice would be create a "class" or new function for the Player object
    name: 'Mr. Jones',
    color: 'blue',
    currentRoom: null,
    requestedSets: [],//or could just be NumRequestedSets
    //Whatever additional game related data could go here
    //Alternately, if the data is specific to the whole game, put it on the room itself
};
players[socket.id] = newPlayer;//Adding this to the players dictionary will let us find the player object again via their socket id

var openRoom = findOpenRoom();
newPlayer.currentRoom = openRoom; //Now the player object has a reference to the game room it's in
openRoom.players.push(newPlayer);
socket.join(openRoom.id);//This will create a new socket.io channel to make it easy to emit to a single room

socket.on('startGame', function (socketId, msg) {
    var myRoom = players[socketId].currentRoom;
    myRoom.started = true;
    myRoom.setCount = 0;//Ensure 0 at start
    //Broadcast the startGame message here,  see below for building gamestate
});

socket.on('requestSet', function (socketId, msg) {

    sendSetOfBoxes(players[socketId]);//Respond to the message here, update the requested set number or whatever

    var gameState = {
        playerData: []
    };
    var myRoom = players[socketId].currentRoom;
    for (var i = 0; i < myRoom.players.length; i++) {
        //Loop over the players, and build a list of players and their history of requested sets
        gameState.playerData.push({
            name: myRoom.players[i].name,
            requestedSetHistory: myRoom.player[i].requestedSets
        })
    }
    socket.broadcast.to(myRoom.id).emit('setWasRequested', gameState);
});
function sendSetOfBoxes(player) {
    //do your sending of boxes here
}
});
var-players={};
var房间={};
房间['randomId236512']={//添加一个示例初学者房间
id:'randomId236512',
名称:'快乐盒子屋',
玩家:[],
开始:错,
集合计数:0
};
函数findOpenRoom(){
//在这里,您可以搜索等待玩家的游戏列表
//如果没有房间,创建一个,将其添加到列表中,然后返回
返回房间[0];
}
io.on('连接',函数(套接字){
var newPlayer={//更好的做法是为Player对象创建一个“类”或新函数
姓名:“琼斯先生”,
颜色:“蓝色”,
当前房间:空,
requestedset:[]、//或只能是numrequestedset
//任何与游戏相关的额外数据都可以放在这里
//或者,如果数据特定于整个游戏,则将其放在房间本身
};
players[socket.id]=newPlayer;//将其添加到players字典将允许我们通过其socket id再次找到player对象
var openRoom=findOpenRoom();
newPlayer.currentRoom=openRoom;//现在玩家对象有了对其所在游戏室的引用
openRoom.players.push(newPlayer);
socket.join(openRoom.id);//这将创建一个新的socket.io通道,以便于向单个房间发射
socket.on('startGame',函数(socketId,msg){
var myRoom=players[socketId].currentRoom;
myRoom.started=true;
myRoom.setCount=0;//开始时确保0
//在这里播放StartName消息,请参见下面的构建游戏状态
});
socket.on('requestSet',函数(socketId,msg){
sendSetOfBoxes(players[socketId]);//在此处响应消息,更新请求的集合号或其他内容
变量配子状态={
玩家数据:[]
};
var myRoom=players[socketId].currentRoom;
对于(var i=0;i
您能显示更多代码吗?在没有看到更多你现在拥有的东西的情况下,很难提出一个好的解决方案。你现在在哪里存储玩家姓名等信息?您的emit代码是什么样子的?