client.on("joinRoom", function(id) {//this id is roomid user wants to join
room = new Room(roomid, client.id);
rooms[roomid] = room;
client.leave(roomid);//this is users own roomid which he leaves to join other room.
room.addPerson(client.id);//adding person to the room's people array using his id, not his roomid.
console.log(rooms[id].people);//list of people in room.
});
这是我的房间
function Room(roomid, owner) {
this.roomid = roomid;
this.owner = owner;
this.people = [];
};
Room.prototype.addPerson = function(personID) {
this.people.push(personID);
};
module.exports = Room;
但是我在people数组中只有一个用户是userid
[ 'vYDNfK4VPchHFwXQPDiI' ]
为什么它无法将其他人ID推送到数组中。
答案 0 :(得分:0)
您正在为每个请求创建一个新房间。在创建之前检查id的房间是否已经存在 -
client.on("joinRoom", function(id) {//this id is roomid user wants to join
if(!rooms[id]) room[id] = new Room(id, client.id);
client.leave(id);//this is users own roomid which he leaves to join other room.
room.addPerson(client.id);//adding person to the room's people array using his id, not his roomid.
console.log(rooms[id].people);//list of people in room.
});