NAME

AnyEvent::WebSocket::Connection - WebSocket connection for AnyEvent

VERSION

version 0.16

SYNOPSIS

# send a message through the websocket...
$connection->send('a message');

# recieve message from the websocket...
$connection->on(each_message => sub {
  # $connection is the same connection object
  # $message isa AnyEvent::WebSocket::Message
  my($connection, $message) = @_;
  ...
});

# handle a closed connection...
$connection->on(finish => sub {
  # $connection is the same connection object
  my($connection) = @_;
  ...
});

# close an opened connection
# (can do this either inside or outside of
# a callback)
$connection->close;

(See AnyEvent::WebSocket::Client on how to create a connection)

DESCRIPTION

This class represents a WebSocket connection with a remote server (or in the future perhaps a client).

If the connection object falls out of scope then the connection will be closed gracefully.

This class was created for a client to connect to a server via AnyEvent::WebSocket::Client, but it may be useful to reuse it for a server to interact with a client if a AnyEvent::WebSocket::Server is ever created (after the handshake is complete, the client and server look pretty much the same).

METHODS

$connection->send($message)

Send a message to the other side. $message may either be a string (in which case a text message will be sent), or an instance of AnyEvent::WebSocket::Message.

$connection->on($event => $cb)

Register a callback to a particular event.

For each event $connection is the AnyEvent::WebSocket::Connection and and $message is an AnyEvent::WebSocket::Message (if available).

each_message

$cb->($connection, $message)

Called each time a message is received from the WebSocket.

next_message

$cb->($connection, $message)

Called only for the next message received from the WebSocket.

finish

$cb->($connection)

Called when the connection is terminated

$connection->close

Close the connection.

DEPRECATED METHODS

The methods in this section are deprecated and may be removed from a future version of this class. They should not be used for new code, and are only remain documented here to aid in understanding legacy code that use them.

$connection->on_each_message($cb)

Register a callback to be called on each subsequent message received. The message itself will be passed in as the only parameter to the callback. The message is a decoded text string.

$connection->on_next_message($cb)

Register a callback to be called the next message received. The message itself will be passed in as the only parameter to the callback. The message is a decoded text string.

$connection->on_finish($cb)

Register a callback to be called when the connection is closed.

SEE ALSO

AUTHOR

author: Graham Ollis <plicease@cpan.org>

contributors:

Toshio Ito

COPYRIGHT AND LICENSE

This software is copyright (c) 2013 by Graham Ollis.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.