lib/auto/share/dist/App-SocialCalc-Multiplayer/third-party/lib/PocketIO/Transport/WebSocket.pm untested

filter: Statementstmt 0.0% 0 / 57 Branchbran 0.0% 0 / 6 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 9 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 75 CC 4 SCAR 20.8
CC
4
Cov
0%
dispatch
20.0
name
2.0
_build_frame
2.0
CRAP 8.0
1 package PocketIO::Transport::WebSocket;
2  
3 use strict;
4 use warnings;
5  
6 use base 'PocketIO::Transport::Base';
7  
8 use Protocol::WebSocket::Frame;
9 use Protocol::WebSocket::Handshake::Server;
10  
11 use PocketIO::Handle;
12  
13 sub name {'websocket'}
14  
15 sub dispatch {
16 my $self = shift;
17 my ($cb) = @_;
18  
19 my $fh = $self->req->env->{'psgix.io'};
20 return unless $fh;
21  
22 my $hs = Protocol::WebSocket::Handshake::Server->new_from_psgi($self->req->env);
23 return unless $hs->parse($fh);
24  
25 return unless $hs->is_done;
26  
27 my $handle = $self->_build_handle($fh);
28 my $frame = Protocol::WebSocket::Frame->new;
29  
30 return sub {
31 my $respond = shift;
32  
33 $handle->write(
34 $hs->to_string => sub {
35 my $handle = shift;
36  
37 my $conn = $self->add_connection(on_connect => $cb);
38  
39 my $close_cb = sub {
40 $handle->close;
41 $self->client_disconnected($conn);
42 };
43 $handle->on_eof($close_cb);
44 $handle->on_error($close_cb);
45  
46 $handle->on_heartbeat(sub { $conn->send_heartbeat });
47  
48 $handle->on_read(
49 sub {
50 $frame->append($_[1]);
51  
52 while (my $message = $frame->next_bytes) {
53 $conn->read($message);
54 }
55 }
56 );
57  
58 $conn->on_write(
59 sub {
60 my $bytes = $self->_build_frame($_[1]);
61  
62 $handle->write($bytes);
63 }
64 );
65  
66 $self->client_connected($conn);
67  
68 $conn->send_id_message($conn->id);
69 }
70 );
71 };
72 }
73  
74 sub _build_frame {
75 my $self = shift;
76 my ($bytes) = @_;
77  
78 return Protocol::WebSocket::Frame->new($bytes)->to_bytes;
79 }
80  
81 1;
82 __END__