Net::Async::WebSocket::Protocol - send and receive JSON-encoded data over WebSockets
Net::Async::WebSocket::Protocol
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 new or configure:
new
configure
Optional. The JSON codec instance. This must support encode and decode methods compatible with those provided by JSON, JSON::XS or similar.
encode
decode
$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.
$text
utf8
If not provided, the JSON::MaybeXS->new constructor is used to find a suitable implementation.
CODE reference for event handler.
The following methods documented with a trailing call to ->get return Future instances.
->get
$self->send_json( $data )->get
Sends a text frame containing a JSON encoding of the Perl data structure provided.
Paul Evans <leonerd@leonerd.org.uk>
To install Net::Async::WebSocket::JSON, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Net::Async::WebSocket::JSON
CPAN shell
perl -MCPAN -e shell install Net::Async::WebSocket::JSON
For more information on module installation, please visit the detailed CPAN module installation guide.