Skip Menu |

This queue is for tickets about the AnyEvent-FreeSWITCH CPAN distribution.

Report information
The Basics
Id: 78069
Status: new
Priority: 0/
Queue: AnyEvent-FreeSWITCH

People
Owner: Nobody in particular
Requestors: LOTTC [...] cpan.org
Cc:
AdminCc:

Bug Information
Severity: Normal
Broken in: (no value)
Fixed in: (no value)



Subject: Example event loop script.
Hello, I put together a small script using your module. I really couldn't find any good examples of how exactly to use your module...and it would be nice to have something like this included with the source. While most of it is pretty straight forward, I ordered the callbacks in such a way that they detect if the connection is lost and reconnect if needed. Anyway, it could help others get up and going faster...feel free to use it. -Lott Caskey
Subject: fsevent-example.pl
#!/usr/bin/perl # fsevent-example.pl # - Lott Caskey <lottcaskey@gmail.com> use strict; use AnyEvent; use AnyEvent::FreeSWITCH; use JSON::XS; use Data::Dumper; use ESL; $|++; my $debug = 0; my $fshost = '127.0.0.1'; my $fsport = '8021'; my $fspass = 'ClueCon'; my $fsevnt = 'all'; my $cv = AnyEvent->condvar; my $fs = AnyEvent::FreeSWITCH->new(host=>$fshost, port=>$fsport, password=>$fspass, events=>$fsevnt); # When "connect" event is called, we call the connect method. $fs->reg_cb( connect => sub { my $self = shift; $self->connect(); } ); # If "connect" was successful, the "connected" event below is triggered and we # send a message to STDERR with the good news. We then setup a timer to check # the connection every 5 seconds. If the connection fails, we alert STDERR # and trigger the "connect" event. $fs->reg_cb( connected => sub { my $self = shift; print STDERR "Connected!\n"; $self->{connection_timer} = AnyEvent->timer( after => 5, interval => 5, cb => sub { unless ($self->is_connected()) { $self->{connection_timer} = undef; print STDERR "Lost Connection, trying to reconnect.\n"; $self->event('connect'); } } ); } ); # If "connect" was not successful, send the given error to STDERR and then # schedule a "connect" event to reattempt. $fs->reg_cb( connection_error => sub { my ($self, $error) = @_; print STDERR $error."\n"; my $wait_one_sec = AnyEvent->timer( after => 2, interva => 0, cb => sub { print STDERR "Retrying...\n"; $self->event('connect'); } ) } ); # Captures each FreeSWITCH event, with "event_name" containing the event # command and "event_headers" containing a hashref of the all the event details. # Setting debug to 1 will output the recieved event names to STDERR and # allow you to figure out which "event_*" callbacks you need to define. # Setting debug to 2 will output the event headers to STDERR for all events. $fs->reg_cb( recv_event => sub { my ($self, $event_name, $data, $blah) = @_; my $event_headers = decode_json($data); print STDERR $self->event_name.': '.$event_name.': '.$event_headers->{'Core-UUID'}."\n" if (defined($self->{debug}) and $self->{debug}>0); print STDERR $self->event_name.': '.Data::Dumper->new([$event_headers])->Indent(1)->Terse(1)->Sortkeys(1)->Dump()."\n\n" if (defined($self->{debug}) and $self->{debug}>1); } ); # Capture individual FreeSWITCH events by using the callback id "event_" and the event name. # The callback "event_HEARTBEAT" is capturing the FreeSWITCH "heartbeat" event and then # displaying the details of the message. $fs->reg_cb( event_HEARTBEAT => sub { my ($self, $data) = @_; my $event_headers = decode_json($data); print STDERR $self->event_name.': '.$event_headers->{'Event-Name'}.': '.$event_headers->{'Core-UUID'}."\n".Data::Dumper->new([$event_headers])->Indent(1)->Terse(1)->Sortkeys(1)->Dump()."\n\n"; } ); $fs->reg_cb( event_API => sub { my ($self, $data) = @_; my $event_headers = decode_json($data); #print STDERR "a"; } ); $fs->reg_cb( event_PRESENCE_IN => sub { my ($self, $data) = @_; my $event_headers = decode_json($data); #print STDERR 'p'; } ); # Register an event to catch any changes to the debug level. $fs->reg_cb( set_debug => sub { my ($self, $debug) = @_; $self->{debug} = (defined($debug) and $debug=~/^[+-]?\d+$/) ? ($debug*1) : 0; }); $fs->event('set_debug', $debug); # Send the "connect" event to start our loop. $fs->event('connect'); print STDERR "Starting Main Loop.\n"; $cv->recv;