line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Alice::HTTP::Stream::WebSocket; |
2
|
|
|
|
|
|
|
|
3
|
2
|
|
|
2
|
|
13
|
use JSON; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
22
|
|
4
|
2
|
|
|
2
|
|
355
|
use Any::Moose; |
|
2
|
|
|
|
|
7
|
|
|
2
|
|
|
|
|
16
|
|
5
|
2
|
|
|
2
|
|
1060
|
use Digest::MD5 qw/md5/; |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
118
|
|
6
|
2
|
|
|
2
|
|
13
|
use Time::HiRes qw/time/; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
21
|
|
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
extends 'Alice::HTTP::Stream'; |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
has fh => ( |
11
|
|
|
|
|
|
|
is => 'ro', |
12
|
|
|
|
|
|
|
required => 1, |
13
|
|
|
|
|
|
|
); |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
has handle => ( |
16
|
|
|
|
|
|
|
is => 'rw', |
17
|
|
|
|
|
|
|
); |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
has on_read => ( |
20
|
|
|
|
|
|
|
is => 'ro', |
21
|
|
|
|
|
|
|
isa => 'CodeRef', |
22
|
|
|
|
|
|
|
); |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
has is_xhr => ( |
25
|
|
|
|
|
|
|
is => 'ro', |
26
|
|
|
|
|
|
|
default => 0, |
27
|
|
|
|
|
|
|
); |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
has ws_version => ( |
30
|
|
|
|
|
|
|
is => 'rw', |
31
|
|
|
|
|
|
|
required => 1, |
32
|
|
|
|
|
|
|
); |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
sub BUILD { |
35
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
36
|
|
|
|
|
|
|
|
37
|
0
|
|
|
|
|
|
my $h = AnyEvent::Handle->new( |
38
|
|
|
|
|
|
|
fh => $self->fh, |
39
|
|
|
|
|
|
|
rbuf_max => 1024 * 10, |
40
|
|
|
|
|
|
|
); |
41
|
|
|
|
|
|
|
|
42
|
0
|
|
|
|
|
|
$h->{ws_version} = $self->ws_version; |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
$h->on_error(sub { |
45
|
0
|
|
|
0
|
|
|
$self->close; |
46
|
0
|
|
|
|
|
|
undef $h; |
47
|
0
|
|
|
|
|
|
$self->on_error->(); |
48
|
0
|
|
|
|
|
|
}); |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
$h->on_eof(sub { |
51
|
0
|
|
|
0
|
|
|
$self->close; |
52
|
0
|
|
|
|
|
|
undef $h; |
53
|
0
|
|
|
|
|
|
$self->on_error->(); |
54
|
0
|
|
|
|
|
|
}); |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
$h->on_read(sub { |
57
|
|
|
|
|
|
|
$_[0]->push_read( |
58
|
|
|
|
|
|
|
'AnyEvent::Handle::Message::WebSocket', |
59
|
0
|
|
|
|
|
|
sub { $self->on_read->(from_json $_[1]) } |
60
|
0
|
|
|
0
|
|
|
); |
61
|
0
|
|
|
|
|
|
}); |
62
|
|
|
|
|
|
|
|
63
|
0
|
|
|
|
|
|
$self->handle($h); |
64
|
0
|
|
|
|
|
|
$self->send([{type => "identify", id => $self->id}]); |
65
|
|
|
|
|
|
|
} |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
sub send { |
68
|
0
|
|
|
0
|
0
|
|
my ($self, $messages) = @_; |
69
|
|
|
|
|
|
|
|
70
|
0
|
0
|
|
|
|
|
$messages = [$messages] unless ref $messages eq "ARRAY"; |
71
|
|
|
|
|
|
|
|
72
|
0
|
|
|
|
|
|
my $line = to_json( |
73
|
|
|
|
|
|
|
{queue => $messages}, |
74
|
|
|
|
|
|
|
{utf8 => 1, shrink => 1} |
75
|
|
|
|
|
|
|
); |
76
|
|
|
|
|
|
|
|
77
|
0
|
|
|
|
|
|
$self->send_raw($line); |
78
|
|
|
|
|
|
|
} |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
sub send_raw { |
81
|
0
|
|
|
0
|
0
|
|
my ($self, $string) = @_; |
82
|
0
|
|
|
|
|
|
$self->handle->push_write( |
83
|
|
|
|
|
|
|
'AnyEvent::Handle::Message::WebSocket', |
84
|
|
|
|
|
|
|
$string |
85
|
|
|
|
|
|
|
); |
86
|
|
|
|
|
|
|
} |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
sub close { |
89
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
90
|
0
|
|
|
|
|
|
$self->handle->destroy; |
91
|
0
|
|
|
|
|
|
$self->closed(1); |
92
|
|
|
|
|
|
|
} |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
__PACKAGE__->meta->make_immutable; |
95
|
|
|
|
|
|
|
1; |