-
Notifications
You must be signed in to change notification settings - Fork 0
/
behavior.js
73 lines (58 loc) · 2.29 KB
/
behavior.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
behavior('/', {
open:function(event){
var pos = event.connection.geo;
pos.id = event.connection.id;
pos.type = "join";
var msg = JSON.stringify(pos);
event.channel.emit(msg);
var pos_list = [];
pos_list.push(event.connection.id);
pos_list.push(pos.longitude);
pos_list.push(pos.latitude);
pos_list.push(pos.city);
pos_list.push(pos.region);
pos_list.push(pos.country_name);
pos_list.push(pos.country_code);
pos_list.push(Math.floor(new Date().getTime() / 1000));
event.channel.set('active:'+event.connection.id, pos_list.join(','));
event.allow(msg);
},
close:function(event){
var msg = {id: event.connection.id, type:"leave"};
event.channel.emit(JSON.stringify(msg));
event.channel.del('active:'+event.connection.id);
var pos = event.connection.geo;
var pos_list = [];
pos_list.push(event.connection.id);
pos_list.push(pos.longitude);
pos_list.push(pos.latitude);
pos_list.push(pos.city);
pos_list.push(pos.region);
pos_list.push(pos.country_name);
pos_list.push(pos.country_code);
pos_list.push(Math.floor(new Date().getTime() / 1000));
event.channel.push('visit', pos_list.join(','));
// trim the "visits" to only contain the last 100 added elements
event.channel.trim('visit', -100);
},
emit: function(event){
switch(event.data){
case "list":
event.channel.range('visit', -100, function(err, items) {
if(err){
console.log(err);
}
event.channel.emit(JSON.stringify({type:"list", items:items}), event.connection);
});
break;
case "active":
event.channel.findall("active:*", function(err, items){
if(err){
console.log(err);
}
event.channel.emit(JSON.stringify({type:"active", items:items}), event.connection);
});
break;
}
}
});