Dancer2-Plugin-WebSocket

 view release on metacpan or  search on metacpan

README.mkdn  view on Meta::CPAN

          };

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

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

true;
```

# DESCRIPTION

`Dancer2::Plugin::WebSocket` provides an interface to [Plack::App::WebSocket](https://metacpan.org/pod/Plack::App::WebSocket)
and allows to interact with the webSocket connections within the Dancer app.

[Plack::App::WebSocket](https://metacpan.org/pod/Plack::App::WebSocket), and thus this plugin, requires a plack server that
supports the psgi _streaming_, _nonblocking_ and _io_. [Twiggy](https://metacpan.org/pod/Twiggy)
is the most popular server fitting the bill.

# CONFIGURATION

- serializer

    If serializer is set to a `true` value, messages will be assumed to be JSON
    objects and will be automatically encoded/decoded using a [JSON::MaybeXS](https://metacpan.org/pod/JSON::MaybeXS)
    serializer.  If the value of `serializer` is a hash, it'll be passed as
    arguments to the [JSON::MaybeXS](https://metacpan.org/pod/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

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

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

- mount\_path

    Path for the websocket mountpoint. Defaults to `/ws`.

# PLUGIN KEYWORDS

In the various callbacks, the connection object `$conn`
is a [Plack::App::WebSocket::Connection](https://metacpan.org/pod/Plack::App::WebSocket::Connection) object
augmented with the [Dancer2::Plugin::WebSocket::Connection](https://metacpan.org/pod/Dancer2::Plugin::WebSocket::Connection) role.

## websocket\_on\_open sub { ... }

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

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

## websocket\_on\_login sub { ... }

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

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

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

```perl
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;
    }
};
```

## websocket\_on\_close sub { ... }

```perl
websocket_on_close sub {
    my( $conn ) = @_;



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