UniEvent::WebSocket - Extremely efficient asynchronous WebSocket Client and Server
# 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;
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
UniEvent::WebSocket supports per-message deflate.
UniEvent::WebSocket
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.
Websocket client is implemented in UniEvent::WebSocket::Client, see its docs for API.
Websocket server is implemented in UniEvent::WebSocket::Server, see its docs for API.
Logs are accessible via XLog framework as "UniEvent::WebSocket" module.
XLog::set_logger(XLog::Console->new); XLog::set_level(XLog::DEBUG, "UniEvent::WebSocket");
These are just an aliases in UniEvent::WebSocket:: namespace from Protocol::WebSocket::Fast, see its docs for details.
UniEvent::WebSocket::
Grigory Smorkalov, Pronin Oleg <syber@cpan.org>, Crazy Panda LTD
You may distribute this code under the same terms as Perl itself.
To install UniEvent::WebSocket, copy and paste the appropriate command in to your terminal.
cpanm
cpanm UniEvent::WebSocket
CPAN shell
perl -MCPAN -e shell install UniEvent::WebSocket
For more information on module installation, please visit the detailed CPAN module installation guide.