AnyEvent::PocketIO::Client - Socket.IO client
# This APIs will be changed. use AnyEvent; use AnyEvent::PocketIO::Client; my $client = AnyEvent::PocketIO::Client->new; $client->on('message' => sub { print STDERR "get message : $_[1]\n"; }); # first handshake, then open. my $cv = AnyEvent->condvar; $client->handshake( $server, $port, sub { my ( $error, $self, $sesid, $hb_timeout, $con_timeout, $trans ) = @_; $client->open( 'websocket' => sub { $self->reg_event('foo' => sub { # ... }); $cv->send; } ); } ); $cv->wait; # ... loop, timer, etc. $client->disconnect; # # OR socket.io client interface # use PocketIO::Client::IO; my $socket = PocketIO::Client::IO->connect("http://localhost:3000/"); my $cv = AnyEvent->condvar; my $w = AnyEvent->timer( after => 5, cb => $cv ); $socket->on( 'message', sub { say $_[1]; } ); $socket->on( 'connect', sub { $socket->send('Parumon!'); $socket->emit('hello', "perl"); } ); $cv->wait;
Socket.IO client using PocketIO and AnyEvent.
This is beta version. APIs will be changed.
Currently acceptable transport id is websocket only.
$client = AnyEvent::PocketIO::Client->new( %opts )
Returns a new object. it can take the follow options
$client->handshake( $host, $port, $cb );
The handshake routine. it executes a call back $cb that takes an error (if any, otherwise undef), client itself, the session id, heartbeat timeout, connection timeout and list reference of transports.
$cb
undef
sub { my ( $error, $client, $sesid, $hb_timeout, $conn_timeout, $trans ) = @_; if ( $error ) { say "code:", $error->{ code }; say "message:", $error->{ message }; } # ... }
$client->open( $transport_id, $cb );
After handshake success, makes a connection to the server. Currently $transport_id (case-insensitive) is websocket only.
handshake
$transport_id
websocket
When the connection is made, $cb is executed. $cb takes error object and client object.
sub { my ( $error, $client ) = @_; if ( $error ) { say "code:", $error->{ code }; say "message:", $error->{ message }; } # ... }
$boolean = $client->is_opend
$client->connect( $endpoint )
This method is for message type connect. If you want to make a connection to the server in real, call open method.
open
$client->disconnect( $endpoint )
Sends message type disconnect to the server and close the socket handle.
$client->reg_event( 'name' => $subref )
Register an event triggered by server's emit.
You should call this method after opened.
$client->emit( 'event_name', @args )
$client->send( 'message' )
$conn = $client->conn; # PocketIO::Connection
$client->on( 'messsage_type' => $cb );
Acceptable types are 'open', 'connect', 'disconnect', 'heartbeat' and 'message'.
my $transport = $client->transport();
Simple client module PocketIO::Client::IO.
use PocketIO::Client::IO; my $socket = PocketIO::Client::IO->connect("http://localhost:3000/"); my $cv = AnyEvent->condvar; my $w = AnyEvent->timer( after => 5, cb => $cv ); $socket->on( 'message', sub { say $_[1]; } ); $socket->on( 'connect', sub { $socket->send('Parumon!'); $socket->emit('hello', "perl"); } ); $cv->wait;
Currently this module supports websocket only. Patches welcome!
AnyEvent, PocketIO, PcketIO::Client::IO
Makamaka Hannyaharamitu, <makamaka[at]cpan.org>
Copyright 2012 by Makamaka Hannyaharamitu
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
To install PocketIO::Client::IO, copy and paste the appropriate command in to your terminal.
cpanm
cpanm PocketIO::Client::IO
CPAN shell
perl -MCPAN -e shell install PocketIO::Client::IO
For more information on module installation, please visit the detailed CPAN module installation guide.