NAME

Net::WebSocket::Server::Connection - A WebSocket connection managed by Net::WebSocket::Server.

SYNOPSIS

Within the connect callback of a Net::WebSocket::Server,

$conn->on(
    utf8 => sub {
        my ($conn, $msg) = @_;
        $conn->send_utf8($msg);
    },
);

DESCRIPTION

This module provides an interface to a WebSocket connection including handshakes and sending / receiving messages. It is constructed by a running Net::WebSocket::Server and passed to the registered connect handler there for configuration.

CONSTRUCTION

Net::WebSocket::Server::Connection->new(%opts)

Creates a new Net::WebSocket::Server::Connection object with the given configuration. This is typically done for you by Net::WebSocket::Server; you rarely need to construct your own explicitly. Takes the following parameters:

socket

The underlying IO::Socket-like object. Once set, this cannot be changed. Required.

server

The associated Net::WebSocket::Server object. Once set, this cannot be changed. Required.

nodelay

A boolean value indicating whether TCP_NODELAY should be set on the socket after the handshake is complete. Default 1. See nodelay().

max_send_size

The maximum size of an outgoing payload. Default Protocol::WebSocket::Frame->new->{max_payload_size}.

When building an outgoing message, this value is passed to new instances of Protocol::WebSocket::Frame as the max_payload_size parameter.

on_$event

The callback to invoke when the given $event occurs, such as ready. See "EVENTS".

METHODS

on(%events)
$connection->on(
    utf8 => sub { ... },
),

Takes a list of $event => $callback pairs; $event names should not include an on_ prefix. See "EVENTS".

server()

Returns the associated Net::WebSocket::Server object.

socket()

Returns the underlying socket object.

ip()

Returns the connected remote IP as a string or '0.0.0.0' with no active connection.

port()

Returns the connected remote port or 0 with no active connection. (This will be some high-numbered port chosen by the remote host; it can be useful during debugging to help humans tell apart connections from the same IP.)

nodelay([$enable])

A boolean value indicating whether TCP_NODELAY should be set on the socket after the handshake is complete. If the handshake is already complete, immediately modifies the socket's TCP_NODELAY setting.

This setting indicates to the operating system that small packets should not be delayed for bundling into fewer, larger packets, but should instead be sent immediately. While enabling this setting can incur additional strain on the network, it tends to be the desired behavior for WebSocket servers, so it is enabled by default.

max_send_size([$size])

Sets the maximum allowed size of an outgoing payload. Returns the current or newly-set value.

When building an outgoing message, this value is passed to new instances of Protocol::WebSocket::Frame as the max_payload_size parameter.

disconnect($code, $reason)

Invokes the registered disconnect handler, sends a close packet with the given $code and $reason, and disconnects the socket.

send_utf8($message)

Sends a utf8 message with the given content. The message will be UTF8-encoded automatically.

send_binary($message)

Sends a binary message with the given content.

send($type, $raw_data)

Sends a message with the given type and content. Typically, one should use the send_utf8() and send_binary() methods instead.

recv()

Attempts to read from the socket, invoking callbacks for any received messages. The associated Net::WebSocket::Server will call this automatically when data is ready to be read.

EVENTS

Attach a callback for an event by either passing on_$event parameters to the constructor or by passing $event parameters to the on() method.

handshake($connection, $handshake)

Invoked when a handshake message has been received from the client; the $handshake parameter is the underlying Protocol::WebSocket::Handshake::Server object. Use this event to inspect the handshake origin, cookies, etc for validity. To abort the handshake process, call $connection->disconnect().

For example:

if ($handshake->req->origin ne $expected_origin) {
  $connection->disconnect();
  return;
}

if ($handshake->req->subprotocol ne $expected_subprotocol) {
  $connection->disconnect();
  return;
}
ready($connection)

Invoked when the handshake has been completed and the connection is ready to send and receive WebSocket messages. Use this event to perform any final initialization or for the earliest chance to send messages to the client.

disconnect($connection, $code, $reason)

Invoked when the connection is disconnected for any reason. The $code and $reason, if any, are also provided. Use this event for last-minute cleanup of the connection, but by this point it may not be safe to assume that sent messages will be received.

utf8($connection, $message)

Invoked when a utf8 message is received from the client. The $message, if any, is decoded and provided.

binary($connection, $message)

Invoked when a binary message is received from the client. The $message, if any, is provided.

pong($connection, $message)

Invoked when a pong message is received from the client. The $message, if any, is provided. If the associated Net::WebSocket::Server object is configured with a nonzero silence_max, this event will also occur in response to the ping messages automatically sent to keep the connection alive.

AUTHOR

Eric Wastl, <topaz at cpan.org>

SEE ALSO

Net::WebSocket::Server

LICENSE AND COPYRIGHT

Copyright 2013 Eric Wastl.

This program is free software; you can redistribute it and/or modify it under the terms of the the Artistic License (2.0). You may obtain a copy of the full license at:

http://www.perlfoundation.org/artistic_license_2_0

Any use, modification, and distribution of the Standard or Modified Versions is governed by this Artistic License. By using, modifying or distributing the Package, you accept this license. Do not use, modify, or distribute the Package, if you do not accept this license.

If your Modified Version has been derived from a Modified Version made by someone other than you, you are nevertheless required to ensure that your Modified Version complies with the requirements of this license.

This license does not grant you the right to use any trademark, service mark, tradename, or logo of the Copyright Holder.

This license includes the non-exclusive, worldwide, free-of-charge patent license to make, have made, use, offer to sell, sell, import and otherwise transfer the Package with respect to any patent claims licensable by the Copyright Holder that are necessarily infringed by the Package. If you institute patent litigation (including a cross-claim or counterclaim) against any party alleging that the Package constitutes direct or contributory patent infringement, then this Artistic License to you shall terminate on the date that such litigation is filed.

Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES. THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.