NAME

Net::Async::WebSocket::Protocol - send and receive JSON-encoded data over WebSockets

DESCRIPTION

This subclass of Net::Async::WebSocket::Protocol provides some conveniences for sending and receiving JSON-encoded data over WebSockets. Principly, it provides one new method, send_json, for encoding Perl values into JSON and sending them, and one new method, on_json, for decoding received JSON content into Perl values when received.

EVENTS

The following events are invoked, either using subclass methods or CODE references in parameters:

on_json

$self->on_json( $data )
$on_json->( $self, $data )

Invoked when a text frame is received and has been decoded from JSON. It is passed the Perl data structure resulting from the decode operation.

PARAMETERS

The following named parameters may be passed to new or configure:

json => OBJECT

Optional. The JSON codec instance. This must support encode and decode methods compatible with those provided by JSON, JSON::XS or similar.

$text = $json->encode( $data )
$data = $json->decode( $text )

Note in particular that the $text strings are Unicode character strings, not UTF-8 encoded byte strings, and therefore the utf8 option must be disabled.

If not provided, the JSON::MaybeXS->new constructor is used to find a suitable implementation.

on_json => CODE

CODE reference for event handler.

METHODS

The following methods documented with a trailing call to ->get return Future instances.

send_json

$self->send_json( $data )->get

Sends a text frame containing a JSON encoding of the Perl data structure provided.

AUTHOR

Paul Evans <leonerd@leonerd.org.uk>