The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

UniEvent::WebSocket - Extremely efficient asynchronous WebSocket Client and Server

SYNOPSIS

    # Client
    my $client = UniEvent::WebSocket::Client->new;
    $client->connect("ws://myserver.com:12345");
    $client->connect_callback(sub {
        my ($client, $connect_response) = @_;
        if ($connect_response->error) { ... }
        $client->send_text("hello");
    });
    $client->message_callback(sub {
        my ($client, $message) = @_;
        say $message->payload;
        $client->close(UniEvent::WebSocket::CLOSE_DONE);
    });
    $client->peer_close_callback(sub {
        my ($client, $message) = @_;
        say $message->close_code;
        say $message->close_message;
    });
    
    ...
    UE::Loop->default->run;

    # Server
    my $server = UniEvent::WebSocket::Server->new({
        locations => [
            {host => "*", port => 80, reuse_port => 1, backlog => 1024},
            {host => "*", port => 443, reuse_port => 1, backlog => 1024, ssl_ctx => $ssl_ctx},
        ],
        max_frame_size   => 10000,
        max_message_size => 100000,
        deflate          => {
            compression_level     => 3,
            compression_threshold => 1000,
        },
    });
    
    $server->connection_callback(sub {
        my ($server, $client) = @_;
        $client->message_callback(sub {
            my ($client, $message) = @_;
            say $message->payload;
        })
        $client->peer_close_callback(sub {
            my ($client, $message) = @_;
            say $message->close_code;
            say $message->close_message;
        });
        
        $client->send_text("hello from server");
        
        push @client, $client;
    });
    $server->run;
    ...
    UE::Loop->default->run;

DESCRIPTION

UniEvent::WebSocket is a perl port of C++ panda::unievent::websocket framework. It contains asynchronous websocket client and server framework.

It is built on top of Protocol::WebSocket::Fast websocket protocol implementation and UniEvent event framework. This library is an UniEvent user, so you need to run UniEvent's loop for it to work. It is also recommended to read Protocol::WebSocket::Fast docs to understand configuration and the API of messages.

UniEvent::WebSocket supports per-message deflate.

It is built on top of UniEvent::HTTP so UniEvent::WebSocket is a http server as well and can serve http requests also. It can be run as a part of complex UniEvent::HTTP server or as standalone websocket server.

You can use UniEvent::HTTP::Manager to run multi-process http/websocket server with process management.

CLIENT

Websocket client is implemented in UniEvent::WebSocket::Client, see its docs for API.

SERVER

Websocket server is implemented in UniEvent::WebSocket::Server, see its docs for API.

LOGS

Logs are accessible via XLog framework as "UniEvent::WebSocket" module.

    XLog::set_logger(XLog::Console->new);
    XLog::set_level(XLog::DEBUG, "UniEvent::WebSocket");

CONSTANTS

These are just an aliases in UniEvent::WebSocket:: namespace from Protocol::WebSocket::Fast, see its docs for details.

OPCODE_CONTINUE
OPCODE_TEXT
OPCODE_BINARY
OPCODE_CLOSE
OPCODE_PING
OPCODE_PONG
CLOSE_DONE
CLOSE_AWAY
CLOSE_PROTOCOL_ERROR
CLOSE_INVALID_DATA
CLOSE_UNKNOWN
CLOSE_ABNORMALLY
CLOSE_INVALID_TEXT
CLOSE_BAD_REQUEST
CLOSE_MAX_SIZE
CLOSE_EXTENSION_NEEDED
CLOSE_INTERNAL_ERROR
CLOSE_TLS

AUTHOR

Grigory Smorkalov, Pronin Oleg <syber@cpan.org>, Crazy Panda LTD

LICENSE

You may distribute this code under the same terms as Perl itself.