NAME

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

VERSION

version 0.26

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 or AnyEvent::WebSocket::Server on how to create a connection)

DESCRIPTION

This class represents a WebSocket connection with a remote server or 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, and was later extended to work on the server side via AnyEvent::WebSocket::Server. Once a WebSocket connection is established, the API for both client and server is identical.

ATTRIBUTES

handle

The underlying AnyEvent::Handle object used for the connection. WebSocket handshake MUST be already completed using this handle. You should not use the handle directly after creating AnyEvent::WebSocket::Connection object.

Usually only useful for creating server connections, see below.

masked

If set to true, it masks outgoing frames. The default is false.

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.

SERVER CONNECTIONS

Although written originally to work with AnyEvent::WebSocket::Client, this class was designed to be used for either client or server WebSocket connections. For details, contact the author and/or take a look at the source for AnyEvent::WebSocket::Client and the examples that come with Protocol::WebSocket.

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.