line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package SocketIO::Emitter; |
2
|
2
|
|
|
2
|
|
13543
|
use strict; |
|
2
|
|
|
|
|
2
|
|
|
2
|
|
|
|
|
44
|
|
3
|
2
|
|
|
2
|
|
5
|
use warnings; |
|
2
|
|
|
|
|
2
|
|
|
2
|
|
|
|
|
56
|
|
4
|
|
|
|
|
|
|
our $VERSION = 0.04; |
5
|
|
|
|
|
|
|
|
6
|
2
|
|
|
2
|
|
1103
|
use Redis; |
|
2
|
|
|
|
|
75232
|
|
|
2
|
|
|
|
|
45
|
|
7
|
2
|
|
|
2
|
|
756
|
use Data::MessagePack; |
|
2
|
|
|
|
|
1498
|
|
|
2
|
|
|
|
|
43
|
|
8
|
|
|
|
|
|
|
|
9
|
2
|
|
|
2
|
|
865
|
use Moo; |
|
2
|
|
|
|
|
17887
|
|
|
2
|
|
|
|
|
8
|
|
10
|
2
|
|
|
2
|
|
3471
|
use namespace::clean; |
|
2
|
|
|
|
|
12993
|
|
|
2
|
|
|
|
|
6
|
|
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
has redis => ( is => 'rw'); |
13
|
|
|
|
|
|
|
has key => ( is => 'rw'); |
14
|
|
|
|
|
|
|
has prefix => ( is => 'rw'); |
15
|
|
|
|
|
|
|
has rooms => ( is => 'rw', default => sub {[]} ); |
16
|
|
|
|
|
|
|
has flags => ( is => 'rw', default => sub {{}} ); |
17
|
|
|
|
|
|
|
has messagepack => ( is => 'rw', default => sub { Data::MessagePack->new() } ); |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
my $UID = 'emitter'; |
20
|
|
|
|
|
|
|
my $EVENT = 2; |
21
|
|
|
|
|
|
|
my $BINARY_EVENT = 5; |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
sub BUILD { |
24
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
25
|
|
|
|
|
|
|
# redis |
26
|
0
|
|
0
|
|
|
|
my $redis = $self->redis || Redis->new(); |
27
|
0
|
|
|
|
|
|
$self->redis($redis); |
28
|
|
|
|
|
|
|
# prefix |
29
|
0
|
0
|
|
|
|
|
$self->prefix($self->key ? $self->key : 'socket.io'); |
30
|
|
|
|
|
|
|
} |
31
|
|
|
|
|
|
|
|
32
|
0
|
|
|
0
|
0
|
|
sub json { $_[0]->flags->{json} = 1; $_[0]; } |
|
0
|
|
|
|
|
|
|
33
|
0
|
|
|
0
|
0
|
|
sub volatile { $_[0]->flags->{volatile} = 1; $_[0]; } |
|
0
|
|
|
|
|
|
|
34
|
0
|
|
|
0
|
0
|
|
sub broadcast { $_[0]->flags->{broadcast} = 1; $_[0]; } |
|
0
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
sub in { |
37
|
0
|
|
|
0
|
0
|
|
my ($self, $room) = @_; |
38
|
0
|
|
|
|
|
|
push @{$self->rooms}, $room |
39
|
0
|
0
|
|
|
|
|
unless grep { $_ eq $room } @{$self->rooms}; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
40
|
0
|
|
|
|
|
|
$self; |
41
|
|
|
|
|
|
|
} |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
sub to { |
44
|
0
|
|
|
0
|
0
|
|
my ($self, $room) = @_; |
45
|
0
|
|
|
|
|
|
$self->in($room); |
46
|
0
|
|
|
|
|
|
$self; |
47
|
|
|
|
|
|
|
} |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
sub of { |
50
|
0
|
|
|
0
|
0
|
|
my ($self, $nsp) = @_; |
51
|
0
|
|
|
|
|
|
$self->flags->{nsp} = $nsp; |
52
|
0
|
|
|
|
|
|
$self; |
53
|
|
|
|
|
|
|
} |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
sub emit { |
56
|
0
|
|
|
0
|
0
|
|
my ($self, @args) = @_; |
57
|
|
|
|
|
|
|
|
58
|
0
|
0
|
|
|
|
|
$self->flags->{nsp} = '/' unless exists $self->{flags}->{nsp}; |
59
|
0
|
|
|
|
|
|
my $chn = $self->prefix . '#' . $self->flags->{nsp} . '#'; |
60
|
|
|
|
|
|
|
|
61
|
0
|
|
|
|
|
|
my $pack_data = $self->pack(@args); |
62
|
0
|
|
|
|
|
|
my $packed = $self->messagepack->utf8->pack($pack_data); |
63
|
|
|
|
|
|
|
|
64
|
0
|
0
|
|
|
|
|
if (scalar @{ $self->rooms }) { |
|
0
|
|
|
|
|
|
|
65
|
0
|
|
|
|
|
|
for my $room (@{ $self->rooms }) { |
|
0
|
|
|
|
|
|
|
66
|
0
|
|
|
|
|
|
my $chn_room = $chn . $room . '#'; |
67
|
0
|
|
|
|
|
|
$self->redis->publish($chn_room, $packed); |
68
|
|
|
|
|
|
|
} |
69
|
|
|
|
|
|
|
} else { |
70
|
0
|
|
|
|
|
|
$self->redis->publish($chn, $packed); |
71
|
|
|
|
|
|
|
} |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
# clear |
74
|
0
|
|
|
|
|
|
$self->clear; |
75
|
|
|
|
|
|
|
|
76
|
0
|
|
|
|
|
|
$self; |
77
|
|
|
|
|
|
|
} |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
sub pack { |
80
|
0
|
|
|
0
|
0
|
|
my ($self, @args) = @_; |
81
|
|
|
|
|
|
|
|
82
|
0
|
|
|
|
|
|
my %packet; |
83
|
0
|
0
|
|
|
|
|
$packet{type} = ($self->include_binary(@args)) ? $BINARY_EVENT : $EVENT; |
84
|
0
|
|
|
|
|
|
$packet{data} = \@args; |
85
|
0
|
|
|
|
|
|
$packet{nsp} = delete $self->flags->{'nsp'}; |
86
|
|
|
|
|
|
|
|
87
|
0
|
|
|
|
|
|
return [$UID, \%packet, { rooms => $self->rooms, flags => $self->flags }]; |
88
|
|
|
|
|
|
|
} |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
sub clear { |
91
|
0
|
|
|
0
|
0
|
|
my ($self) = @_; |
92
|
|
|
|
|
|
|
|
93
|
0
|
|
|
|
|
|
$self->rooms([]); |
94
|
0
|
|
|
|
|
|
$self->flags({}); |
95
|
|
|
|
|
|
|
} |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
sub include_binary { |
98
|
0
|
|
|
0
|
0
|
|
my ($self, @args) = @_; |
99
|
0
|
|
|
|
|
|
for(@args) { |
100
|
0
|
0
|
0
|
|
|
|
return 1 if $_ && /[[:^ascii:]]/; |
101
|
|
|
|
|
|
|
} |
102
|
0
|
|
|
|
|
|
return; |
103
|
|
|
|
|
|
|
} |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
1; |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
__END__ |