-
Notifications
You must be signed in to change notification settings - Fork 2
/
room.js
47 lines (46 loc) · 1.29 KB
/
room.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/**
* 房间
*/
const {MSG,actionType} = require('./message');
class Room {
constructor(roomId, user, io) {
this.peoples = [];
this.io = io;
this.roomId = roomId;
}
sendMsg(msg) {
msg.roomId = this.roomId;
this.io.to(this.roomId).emit('message',msg);
}
add(user,client) {
let roomId = this.roomId;
client.join(roomId, (a, b) => {
let rooms = Object.keys(client.rooms);
console.log(rooms);
user.updateRoom(roomId,client)
this.peoples.push(user);
console.log(roomId + '新的用户加入', user)
this.sendMsg(new MSG(actionType.join,{
user:user.uid,
peoples:this.mapPeoples()
}));
})
}
mapPeoples(){
return this.peoples.map(user=>user.info)
}
leave(user) {
this.peoples.forEach((item, index) => {
if (user.uid === item.uid) {
this.peoples.splice(index, 1);
user.client.leave(user.roomId,()=>{
this.sendMsg(new MSG(actionType.leave,{
user:user.uid,
peoples:this.mapPeoples()
}))
})
}
})
}
}
module.exports = Room;