Dancer2-Plugin-WebSocket

 view release on metacpan or  search on metacpan

lib/Dancer2/Plugin/WebSocket.pm  view on Meta::CPAN

                mySocket.send('hello'); }, 2000 );
            };

      </script></head>
      <body><h1>WebSocket client</h1></body>
    </html>
  END
  };

  get '/say_hi' => sub {
    $_->send([ "Hello!" ]) for websocket_connections;
  };

  true;

=head1 DESCRIPTION

C<Dancer2::Plugin::WebSocket> provides an interface to L<Plack::App::WebSocket>
and allows to interact with the webSocket connections within the Dancer app.

L<Plack::App::WebSocket>, and thus this plugin, requires a plack server that
supports the psgi I<streaming>, I<nonblocking> and I<io>. L<Twiggy>
is the most popular server fitting the bill.

=head1 CONFIGURATION

=over

=item serializer

If serializer is set to a C<true> value, messages will be assumed to be JSON
objects and will be automatically encoded/decoded using a L<JSON::MaybeXS>
serializer.  If the value of C<serializer> is a hash, it'll be passed as
arguments to the L<JSON::MaybeXS> constructor.

    plugins:
        WebSocket:
            serializer:
                utf8:         1
                allow_nonref: 1

By the way, if you want the connection to automatically serialize data
structures to JSON on the client side, you can do something like

    var mySocket = new WebSocket(urlMySocket);
    mySocket.sendJSON = function(message) {
        return this.send(JSON.stringify(message))
    };

    // then later...
    mySocket.sendJSON({ whoa: "auto-serialization ftw!" });

=item mount_path

Path for the websocket mountpoint. Defaults to C</ws>.

=back

=head1 PLUGIN KEYWORDS

In the various callbacks, the connection object C<$conn>
is a L<Plack::App::WebSocket::Connection> object
augmented with the L<Dancer2::Plugin::WebSocket::Connection> role.

=head2 websocket_on_open sub { ... }

    websocket_on_open sub {
        my( $conn, $env ) = @_;
        ...;
    };

Code invoked when a new socket is opened. Gets the new
connection
object and the Plack
C<$env> hash as arguments.

=head2 websocket_on_login sub { ... }

    websocket_on_login sub {
        my( $conn, $env ) = @_;
        ...;
    };

Code invoked when a new socket is opened. Gets the
connection object and the Plack C<$env> hash as arguments.

Example: return true if user is logged in and the webapp http_cookie is the same as the websocket.

    my $login_conn;
    my $cookie_name = 'example.session';

    hook before => sub {
        if (defined cookies->{$cookie_name}) {
            $login_conn->{'cookie_id'} = cookies->{$cookie_name}->value;
        }
        $login_conn->{'login'} = logged_in_user ? 1 : 0;
    };

    websocket_on_login sub {
        my( $conn, $env ) = @_;

        my ($cookie_id) = ($env->{'HTTP_COOKIE'} =~ /$cookie_name=(.*);?/g);
        if (($login_conn->{'login'}) and ($login_conn->{'cookie_id'} eq $cookie_id)) {
            return 1;
        } else {
            warn "require login";
            return 0;
        }
    };

=head2 websocket_on_close sub { ... }

    websocket_on_close sub {
        my( $conn ) = @_;
        ...;
    };

Code invoked when a new socket is opened. Gets the
connection object as argument.

=head2 websocket_on_error sub { ... }



( run in 0.505 second using v1.01-cache-2.11-cpan-39bf76dae61 )