line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
# You may distribute under the terms of either the GNU General Public License |
2
|
|
|
|
|
|
|
# or the Artistic License (the same terms as Perl itself) |
3
|
|
|
|
|
|
|
# |
4
|
|
|
|
|
|
|
# (C) Paul Evans, 2010-2014 -- leonerd@leonerd.org.uk |
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
package Net::Async::WebSocket::Server; |
7
|
|
|
|
|
|
|
|
8
|
3
|
|
|
3
|
|
96387
|
use strict; |
|
3
|
|
|
|
|
13
|
|
|
3
|
|
|
|
|
70
|
|
9
|
3
|
|
|
3
|
|
12
|
use warnings; |
|
3
|
|
|
|
|
5
|
|
|
3
|
|
|
|
|
66
|
|
10
|
3
|
|
|
3
|
|
12
|
use base qw( IO::Async::Listener ); |
|
3
|
|
|
|
|
5
|
|
|
3
|
|
|
|
|
1209
|
|
11
|
|
|
|
|
|
|
|
12
|
3
|
|
|
3
|
|
20422
|
use Carp; |
|
3
|
|
|
|
|
5
|
|
|
3
|
|
|
|
|
148
|
|
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
our $VERSION = '0.12'; |
15
|
|
|
|
|
|
|
|
16
|
3
|
|
|
3
|
|
375
|
use Net::Async::WebSocket::Protocol; |
|
3
|
|
|
|
|
7
|
|
|
3
|
|
|
|
|
86
|
|
17
|
|
|
|
|
|
|
|
18
|
3
|
|
|
3
|
|
1119
|
use Protocol::WebSocket::Handshake::Server; |
|
3
|
|
|
|
|
15258
|
|
|
3
|
|
|
|
|
914
|
|
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
=head1 NAME |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
C - serve WebSocket clients using C |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
=head1 SYNOPSIS |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
use IO::Async::Loop; |
27
|
|
|
|
|
|
|
use Net::Async::WebSocket::Server; |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
my $server = Net::Async::WebSocket::Server->new( |
30
|
|
|
|
|
|
|
on_client => sub { |
31
|
|
|
|
|
|
|
my ( undef, $client ) = @_; |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
$client->configure( |
34
|
|
|
|
|
|
|
on_text_frame => sub { |
35
|
|
|
|
|
|
|
my ( $self, $frame ) = @_; |
36
|
|
|
|
|
|
|
$self->send_text_frame( $frame ); |
37
|
|
|
|
|
|
|
}, |
38
|
|
|
|
|
|
|
); |
39
|
|
|
|
|
|
|
} |
40
|
|
|
|
|
|
|
); |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
my $loop = IO::Async::Loop->new; |
43
|
|
|
|
|
|
|
$loop->add( $server ); |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
$server->listen( |
46
|
|
|
|
|
|
|
service => 3000, |
47
|
|
|
|
|
|
|
)->get; |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
$loop->run; |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
=head1 DESCRIPTION |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
This subclass of L accepts WebSocket connections. When a |
54
|
|
|
|
|
|
|
new connection arrives it will perform an initial handshake, and then pass the |
55
|
|
|
|
|
|
|
connection on to the continuation callback or method. |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
=cut |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
=head1 EVENTS |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
The following events are invoked, either using subclass methods or CODE |
62
|
|
|
|
|
|
|
references in parameters: |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
=head2 on_client |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
$self->on_client( $client ) |
67
|
|
|
|
|
|
|
$on_client->( $self, $client ) |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
Invoked when a new client connects and completes its initial handshake. |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
It will be passed a new instance of a L |
72
|
|
|
|
|
|
|
object, wrapping the client connection. |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
=head2 on_handshake |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
Invoked when a handshake has been requested. |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
$self->on_handshake( $client, $hs, $continue ) |
79
|
|
|
|
|
|
|
$on_handshake->( $self, $client, $hs, $continue ) |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
Calling C<$continue> with a true value will complete the handshake, false will |
82
|
|
|
|
|
|
|
drop the connection. |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
This is useful for filtering on origin, for example: |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
on_handshake => sub { |
87
|
|
|
|
|
|
|
my ( $self, $client, $hs, $continue ) = @_; |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
$continue->( $hs->req->origin eq "http://localhost" ); |
90
|
|
|
|
|
|
|
} |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
=cut |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
sub new |
95
|
|
|
|
|
|
|
{ |
96
|
2
|
|
|
2
|
1
|
8059
|
my $class = shift; |
97
|
2
|
|
|
|
|
27
|
return $class->SUPER::new( |
98
|
|
|
|
|
|
|
handle_class => "Net::Async::WebSocket::Protocol", |
99
|
|
|
|
|
|
|
@_, |
100
|
|
|
|
|
|
|
); |
101
|
|
|
|
|
|
|
} |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
sub on_accept |
104
|
|
|
|
|
|
|
{ |
105
|
2
|
|
|
2
|
1
|
412
|
my $self = shift; |
106
|
2
|
|
|
|
|
4
|
my ( $client ) = @_; |
107
|
|
|
|
|
|
|
|
108
|
2
|
|
|
|
|
18
|
my $hs = Protocol::WebSocket::Handshake::Server->new; |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
$client->configure( |
111
|
|
|
|
|
|
|
on_read => sub { |
112
|
2
|
|
|
2
|
|
2162
|
my ( $client, $buffref, $closed ) = @_; |
113
|
|
|
|
|
|
|
|
114
|
2
|
|
|
|
|
11
|
$hs->parse( $$buffref ); # modifies $$buffref |
115
|
|
|
|
|
|
|
|
116
|
2
|
50
|
|
|
|
1543
|
if( $hs->is_done ) { |
117
|
|
|
|
|
|
|
my $on_handshake = $self->can_event( "on_handshake" ) || |
118
|
2
|
|
100
|
|
|
24
|
sub { $_[3]->( 1 ) }; |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
$on_handshake->( $self, $client, $hs, sub { |
121
|
2
|
|
|
|
|
21
|
my ( $ok ) = @_; |
122
|
|
|
|
|
|
|
|
123
|
2
|
50
|
|
|
|
14
|
unless( $ok ) { |
124
|
0
|
|
|
|
|
0
|
$self->remove_child( $client ); |
125
|
0
|
|
|
|
|
0
|
return; |
126
|
|
|
|
|
|
|
} |
127
|
|
|
|
|
|
|
|
128
|
2
|
|
|
|
|
10
|
$client->configure( on_read => undef ); |
129
|
2
|
|
|
|
|
141
|
$client->write( $hs->to_string ); |
130
|
|
|
|
|
|
|
|
131
|
2
|
|
|
|
|
1355
|
$client->debug_printf( "HANDSHAKE done" ); |
132
|
2
|
|
|
|
|
15
|
$self->invoke_event( on_client => $client ); |
133
|
2
|
|
|
|
|
32
|
} ); |
134
|
|
|
|
|
|
|
} |
135
|
|
|
|
|
|
|
|
136
|
2
|
|
|
|
|
172
|
return 0; |
137
|
|
|
|
|
|
|
}, |
138
|
2
|
|
|
|
|
26
|
); |
139
|
|
|
|
|
|
|
|
140
|
2
|
|
|
|
|
120
|
$self->add_child( $client ); |
141
|
|
|
|
|
|
|
} |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
=head1 PARAMETERS |
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
The following named parameters may be passed to C or C: |
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
=over 8 |
148
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
=item on_client => CODE |
150
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
=item on_handshake => CODE |
152
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
CODE references for event handlers. |
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
=back |
156
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
=cut |
158
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
sub configure |
160
|
|
|
|
|
|
|
{ |
161
|
2
|
|
|
2
|
1
|
67
|
my $self = shift; |
162
|
2
|
|
|
|
|
6
|
my %params = @_; |
163
|
|
|
|
|
|
|
|
164
|
2
|
|
|
|
|
6
|
foreach (qw( on_client on_handshake )) { |
165
|
4
|
100
|
|
|
|
15
|
$self->{$_} = delete $params{$_} if exists $params{$_}; |
166
|
|
|
|
|
|
|
} |
167
|
|
|
|
|
|
|
|
168
|
2
|
|
|
|
|
12
|
$self->SUPER::configure( %params ); |
169
|
|
|
|
|
|
|
} |
170
|
|
|
|
|
|
|
|
171
|
|
|
|
|
|
|
sub listen |
172
|
|
|
|
|
|
|
{ |
173
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
174
|
0
|
|
|
|
|
|
my %params = @_; |
175
|
|
|
|
|
|
|
|
176
|
0
|
|
|
|
|
|
$self->SUPER::listen( |
177
|
|
|
|
|
|
|
socktype => 'stream', |
178
|
|
|
|
|
|
|
%params, |
179
|
|
|
|
|
|
|
); |
180
|
|
|
|
|
|
|
} |
181
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
=head1 AUTHOR |
183
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
Paul Evans |
185
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
=cut |
187
|
|
|
|
|
|
|
|
188
|
|
|
|
|
|
|
0x55AA; |