/
behaviour.js
171 lines (131 loc) · 5.02 KB
/
behaviour.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
const ADMIN_TOKEN = '!<admin-token>';
const ENTRY_POINT = '/streams';
const KEY_CHANNELS = 'channels:';
const KEY_COUNT = 'usercount';
const KEY_ADMIN = 'admin';
const KEY_STREAM = 'stream';
const KEY_DELSTREAM = 'del';
register('videostream-lobby', {
open: function (event) {
event.channel.findall(KEY_CHANNELS + '*', function (err, keys) {
// err is currenly always <null>
keys = keys.sort();
event.allow(keys.join(','));
});
}
});
register('videostream-channel', {
open: function (event) {
var channel = event.channel;
var connid = String(event.connection.id);
var lobby = event.domain.getChannel(ENTRY_POINT);
var channelid = event.params.id;
var channelkey = KEY_CHANNELS + channelid;
function ASSERT (err) {
if (err) {
event.deny('ERR: ' + err);
}
}
function isAdminChannel () {
return channelid.substr(0, ADMIN_TOKEN.length) !== ADMIN_TOKEN;
}
// We are using special charachters in matching in other places,
// so we need to validate channel id.
if (/,|\s/.test(channelid) || (!isAdminChannel() && channelid[0] == '!')) {
event.deny('ERR: Invalid channel id');
}
switch (true) {
// Open channel as a broadcaster. Deny request if channel
// already exists in lobby
case event.write && event.emit && !event.read:
lobby.get(channelkey, function (err, adminid) {
if (err != 'not found') {
ASSERT(err);
}
if (adminid != null) {
ASSERT('Already broadcasting');
}
channel.incr(KEY_COUNT, function (err, count) {
ASSERT(err);
channel.set("admin", connid, function (err) {
ASSERT(err);
lobby.set(channelkey, channelid + ":" + event.token, function (err) {
ASSERT(err);
channel.emit(JSON.stringify({
type: KEY_COUNT,
count: count
}));
lobby.emit(JSON.stringify({
type: KEY_STREAM,
id: channelid,
name: event.token
}));
event.connection.set("counted", "yes", function (err) {
ASSERT(err);
event.allow(String(count));
});
});
});
});
});
break;
// Open channel in subscriber mode. We do not check if channel
// exist, just allow it with a count on number of users currently
// watching the broadcast.
default:
case event.read && event.emit:
channel.incr(KEY_COUNT, function (err, count) {
ASSERT(err);
channel.emit(JSON.stringify({
type: KEY_COUNT,
count: count
}));
event.connection.set("counted", "yes", function (err) {
ASSERT(err);
event.allow(String(count));
});
});
break;
}
},
emit: function (event) {
var channel = event.channel;
channel.emit(event.data);
},
close: function (event) {
var channel = event.channel;
var connid = String(event.connection.id);
var lobby = event.domain.getChannel(ENTRY_POINT);
var channelkey = KEY_CHANNELS + event.params.id;
event.connection.get("counted", function (err, value) {
if (err || value !== "yes") {
return;
}
channel.decr(KEY_COUNT, function (err, count) {
if (err) {
return;
}
// Check if we are broadcast user, if so, delete
// key, or if user count is 0.
channel.get("admin", function (err, adminid) {
if (err) {
return;
}
if (count == 0 || adminid == connid) {
lobby.del(channelkey);
lobby.emit(JSON.stringify({
type: KEY_DELSTREAM,
id: event.params.id
}));
}
channel.emit(JSON.stringify({
type: KEY_COUNT,
count: count
}));
});
});
});
}
});
behavior(ENTRY_POINT, 'videostream-lobby');
behavior(ENTRY_POINT + '/{id}', 'videostream-channel');