Net::Async::WebSocket::Protocol - send and receive JSON-encoded data over WebSockets
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.
The following events are invoked, either using subclass methods or CODE references in parameters:
$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.
The following named parameters may be passed to
- json => OBJECT
$text = $json->encode( $data ) $data = $json->decode( $text )
Note in particular that the
$textstrings are Unicode character strings, not UTF-8 encoded byte strings, and therefore the
utf8option 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.
The following methods documented with a trailing call to
->get return Future instances.
$self->send_json( $data )->get
Sends a text frame containing a JSON encoding of the Perl data structure provided.
Paul Evans <email@example.com>