File Coverage

example/lib/MyApp.pm
Criterion Covered Total %
statement 6 6 100.0
branch n/a
condition n/a
subroutine 2 2 100.0
pod n/a
total 8 8 100.0


line stmt bran cond sub pod time code
1             package MyApp;
2              
3 1     1   623 use Dancer2;
  1         700573  
  1         8  
4              
5 1     1   174163 use Dancer2::Plugin::WebSocket;
  1         5  
  1         16  
6              
7             websocket_on_open sub {
8             my( $conn, $env ) = @_;
9             warn "# opening $conn\n";
10             };
11              
12              
13             websocket_on_close sub {
14             my( $conn ) = @_;
15             warn "# closing $conn\n";
16             };
17              
18             websocket_on_error sub {
19             my ( $env ) = @_;
20             warn "Something went bonker";
21             };
22              
23             websocket_on_message sub {
24             my( $conn, $message ) = @_;
25              
26             if ( $message->{hello} ) {
27             $message->{hello} = 'browser!';
28             $conn->send( $message );
29             }
30              
31             if( my $browser = $message->{browser} ) {
32             $conn->add_channels( $browser );
33             }
34              
35             if ( my $channel = $message->{emit} ) {
36             $conn->to($channel)->send({ emitting => $channel });
37             }
38              
39             if ( my $channel = $message->{broadcast} ) {
40             $conn->to($channel)->broadcast({ broadcasting => $channel });
41             }
42             };
43              
44             get '/' => sub {
45             my $ws_url = websocket_url;
46             return <<"END";
47             <html>
48             <head>
49             <script>
50             var urlMySocket = "$ws_url";
51              
52             var mySocket = new WebSocket(urlMySocket);
53             mySocket.sendJSON = function(message) { return this.send(JSON.stringify(message)) };
54              
55             mySocket.onmessage = function (evt) {
56             console.log( "Got message ", evt.data );
57             // mySocket.close();
58             };
59              
60             mySocket.onopen = function(evt) {
61             console.log("opening");
62             let isChrome = /Chrome/.test(navigator.userAgent) && /Google Inc/.test(navigator.vendor);
63             mySocket.sendJSON({
64             browser: isChrome ? 'chrome' : 'firefox'
65             })
66             setTimeout( function() { mySocket.sendJSON({"hello": "Dancer"}); }, 2000 );
67             };
68              
69             </script>
70             </head>
71             <body>
72             <h1>WebSocket client</h1>
73             </body>
74             </html>
75             END
76             };
77              
78             get '/say_hi' => sub {
79             $_->send([ "Hello!" ]) for websocket_connections;
80             };
81              
82             1;