2014-02-21 7 views
0
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. 
}); 

에서 제대로 작동하지 밀어 나의 룸자바 스크립트가 여기 노드 JS

function Room(roomid, owner) { 
    this.roomid = roomid; 
    this.owner = owner; 
    this.people = []; 
}; 
Room.prototype.addPerson = function(personID) { 
    this.people.push(personID); 
}; 
module.exports = Room; 

입니다하지만 난 그것을 할 수없는 이유는 사용자가 사람들의 배열에 하나의 사용자가

[ 'vYDNfK4VPchHFwXQPDiI' ] 

을 ownid 수 다른 사람들을 ID 배열로 밀어 넣는다.

답변

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. 
}); 
+0

방을 새로 = room (roomid, client.id); 객실 [roomid] = room; outside client.on ("joinRoom".... 방금 방금 그 방을 어떻게 만들 었는지 보여주었습니다. – TeamA1

+1

처음 만들어야하기 때문에 외부에 둘 필요가 없습니다. 수표를 넣어서 재 할당하지 않고 함수 입력으로 'id'를 가져 오지만 익명 함수 안의 모든 곳에서 roomid를 사용합니다. 푸시 기능에는 아무런 문제가 없습니다. –