/
session.js
60 lines (47 loc) · 981 Bytes
/
session.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
48
49
50
51
52
53
54
55
56
57
58
59
60
import sprintf from 'sprintf';
import _ from 'lodash';
var list = []
export default class Session {
constructor () {
var random
do {
random = sprintf('%04d', Math.floor(Math.random() * 10000 - 1));
} while ( list.indexOf(random) >= 0 );
this._id = random;
this._users = {};
list.push(this);
}
get id() {
return this._id;
}
get users() {
return _.values(this._users);
}
join(socket_id, name) {
this._users[socket_id] = {name: name};
}
remove(socket_id) {
delete this._users[socket_id];
}
find_user(socket_id) {
return this._users[socket_id];
}
static list() {
return list;
}
static isExist(id) {
console.log(this.find(id));
return this.find(id) != null;
}
static find(id) {
var data = null
_.each(list, (session) => {
console.log(session.id)
if ( session.id == id ) {
data = session;
return false;
}
});
return data;
}
}