-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchannel.js
100 lines (96 loc) · 2.29 KB
/
channel.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
'use strict';
var config=require('./config.js');
var db=require('./db.js');
var channel={};
var channelList=new Map();
channel.create=function(name,callback){
db.createChannel(name,function(channelId){
new Channel(channelId,name);
callback(channelId);
});
}
channel.loadAll=function(callback){
if(channelList.size) return;
db.getAllChannel(function(result){
for(let ch of result)
new Channel(ch.channelId,ch.name);
callback();
});
}
channel.findById= (channelId) => channelList.get(channelId);
channel.list=function(){
let list=[];
for(let ch of channelList.values())
list.push(ch);
return list;
}
channel.send=function(data){
if(!(Buffer.isBuffer(data) || typeof(data)==='string'))
data=JSON.stringify(data);
for(let ch of channelList.values())
ch.send(data);
}
function Channel(channelId,name){
this.channelId=channelId;
this.name=name;
this.lock=false;
this.onlineList=new Set();
this.onlineMax=config.channelUserMax;
channelList.set(this.channelId,this);
}
Channel.prototype.delete=function(callback){
if(this.onlineList.size)
throw new Error('channel not empty')
var _=this;
db.deleteChannel(this.channelId,function(){
channelList.delete(_.channelId);
callback();
});
}
Channel.prototype.exit=function(user){
if(this.onlineList.has(user)){
this.onlineList.delete(user);
user.channel=null;
this.send({
'action': 'channel_exit',
'userId': user.userId
});
}
return this;
}
Channel.prototype.join=function(user,force){
if(this.lock) return false;
if(this.onlineList.has(user)) return true;
if(this.onlineMax<=this.onlineList.size && !force) return false;
if(user.channel) user.channel.exit(user);
this.send({
'action': 'channel_join',
'userId': user.userId
});
this.onlineList.add(user);
user.channel=this;
return true;
}
Channel.prototype.list=function(){
let list=[];
for(let u of this.onlineList)
list.push(u);
return list;
}
Channel.prototype.send=function(data){
if(!(Buffer.isBuffer(data) || typeof(data)==='string'))
data=JSON.stringify(data);
for(let client of this.onlineList)
client.send(data);
return this;
}
Channel.prototype.update=function(name,callback){
if(name.length===0)
throw new Error('field format error');
var _=this;
db.updateChannel(this.channelId,name,function(){
_.name=name;
callback();
});
}
module.exports=channel;