[Freeswitch-users] Mod Event Socket

Michael Collins msc at freeswitch.org
Fri Oct 29 16:46:06 PDT 2010


Did you properly build the php ESL module?
http://wiki.freeswitch.org/wiki/Esl#Installation
In libs/esl you need to "make" and "make phpmod-install"

Note: If your php script is running on a system that does not have
FreeSWITCH then you will need to download and build ESL on the machine where
the script is running, or you need to move the necessary *.so files over to
that target machine.

Also, I see that you mention that you are running this in a browser. I'm not
sure that is what you need to be doing. I'll defer to more experienced Web
programmers. However this much I can tell you: if your php script isn't
actively running and waiting for a socket connection on port 9090 then this
whole thing will fail completely. Using the socket app from the dialplan
implies that the program on the other end will be sitting around waiting for
a phone call to come in while listening on the port in question and will
actively control the call. The test script you pasted here is the exact
opposite of that: it is a script that connects *to* FreeSWITCH, not one that
sits around waiting for a connection *from* FreeSWITCH.

I think you may need to break down your project into some smaller pieces.
The first thing is to do is define the scope of your project: what all does
it need to do? Does it need to answer a call and control it? If so you need
a daemon of some sort that sits around waiting for calls from FS. Do you
need to update a Web page? If so then you'll need to have some way for the
Web server to communicate either with FreeSWITCH directly or with your
daemon, or with a 3rd party data source, or whatever.

You might want to join #freeswitch at irc.freenode.net so that you can
connect with other FS users and developers. You also should consider
#fusionpbx and #2600hz because both of those channels are frequented by
programmers who are familiar with PHP Web development and FreeSWITCH.

Hope this helps.
-MC

On Fri, Oct 29, 2010 at 1:01 PM, Will Smith <willbelair at yahoo.com> wrote:

> hi Michael,
> Thank you for your input. Really appreciate when having someone giving help
> when in need.
> I bought FS ebook, read chapter 9 and 10. And did what you said:
> 1- put the event_socket_conf.xml back to original state, just modify the ip
> address, because I work through a vpn network. Here , could you confirm that
> mod_event_socket supports different browsers ? This is really important to
> me, because if client could not access from www, there is no point to work
> on this. Thank you.
> -------- the event_socket_conf.xml
> <configuration name="event_socket.conf" description="Socket Client">
>   <settings>
>     <param name="nat-map" value="false"/>
>     <param name="listen-ip" value="192.168.51.133"/>
>     <param name="listen-port" value="8021"/>
>     <param name="password" value="ClueCon"/>
>     <!--param name="apply-inbound-acl" value="lan"/-->
>   </settings>
> </configuration>
> -----------
>
> 2- I put this in both dialplan/default.xml  and dialplan/public.xml
> ------------
> <extension name="outbound-socket">
> <condition field="destination_number" expression="^444$">
> <action application="socket" data="192.168.51.133:8021 async full"/>
> </condition>
> </extension>
> ------------
>
> 3- I have a simple php script just to connect to the FS, this is a sample
> script from FS wiki page
> ------
> <?php
>
> //require_once('ESL.php');
>
> require_once('/usr/src/freeswitch/freeswitch-snapshot/libs/esl/php/ESL.php');
> set_time_limit(0); // Remove the PHP time limit of 30 seconds for
> completion due to loop watching events
> echo "HERE";
> // Connect to FreeSWITCH
> $sock = new ESLconnection('localhost', '8021', 'ClueCon');
> // We want all Events (probably will want to change this depending on your
> needs)
> $sock->sendRecv("event plain ALL");
>
>
> // Grab Events until process is killed
> //while($sock->connected()){
> //echo "CONECTED";
> //$event = $sock->recvEvent();
> //print_r($event->serialize());
> //}
>
> ?>
> ------
>
> when running this from web browser, I got blank page. So, I run from
> console, this is the error messages:
> --------
> PHP Warning:  dl(): Unable to load dynamic library
> '/usr/lib/php/modules/ESL.so' - /usr/lib/php/modules/ESL.so: cannot open
> shared object file: No such file or directory in
> /usr/src/freeswitch/freeswitch-snapshot/libs/esl/php/ESL.php on line 23
> PHP Stack trace:
> PHP   1. {main}() /var/www/html/eventsocket.php:0
> PHP   2. require_once() /var/www/html/eventsocket.php:4
> PHP   3. dl()
> /usr/src/freeswitch/freeswitch-snapshot/libs/esl/php/ESL.php:23
> HEREPHP Fatal error:  Call to undefined function new_ESLconnection() in
> /usr/src/freeswitch/freeswitch-snapshot/libs/esl/php/ESL.php on line 119
> PHP Stack trace:
> PHP   1. {main}() /var/www/html/eventsocket.php:0
> PHP   2. ESLconnection->__construct() /var/www/html/eventsocket.php:8
> ----------
>
> So, I guess the ESL.so is missing. I tried to  "locate ESL.so" , but
> nothing returned. I tried to
> --------
>
> yum install libxml2-devel pcre-devel bzip2-devel curl-devel gmp-devel aspell-devel php-devel libtermcap-devel gdbm-devel
>  db4-devel
>
> --------
> it ran fine. But I did not do this:
> -------
> *NOTE:* For PHP you must edit the libs/esl/php/Makefile and add -lpthread
> to the LOCAL_LDFLAGS line.
> -------
> because I could not fine the LOCAL_LDFLAGS line.
>
> Please give me some help, I am banging my head against the wall here. If I
> could get the file ESL.so , is the set up above working? I thought it would
> be simple just to connect.
>
> Thank you.
>
> --- On *Thu, 10/28/10, Michael Collins <msc at freeswitch.org>* wrote:
>
>
> From: Michael Collins <msc at freeswitch.org>
> Subject: Re: [Freeswitch-users] Mod Event Socket
> To: "FreeSWITCH Users Help" <freeswitch-users at lists.freeswitch.org>
> Received: Thursday, October 28, 2010, 5:57 PM
>
>
> Be sure to learn the difference between inbound and outbound event socket.
> In your case you are doing outbound event socket. The dialplan calls the
> socket app which does an outbound socket connection to port 9090 on the
> localhost. Make sure that your app is listening on port 9090.
>
> The event_socket.conf.xml file is for configuring inbound socket
> connections, e.g. when you have a script that attempts to connect to FS
> where FS is listening on port 8021. So change your event_socket.conf.xml
> file back to the default and retry.
>
> Report back here if you have trouble. Also, buy or borrow the FreeSWITCH
> book and check out chapter 9. Lots of good stuff there on how the even
> system works.
>
> -MC
>
> On Thu, Oct 28, 2010 at 2:14 PM, Will Smith <willbelair at yahoo.com<http://mc/compose?to=willbelair@yahoo.com>
> > wrote:
>
> Hi,
> I am new to FS, and trying to get the mod event socket installed and
> running. I have FS running, with SIP account, can dial in/out via gateway.
> Now I want to dial in, FS will send some info to client browser, here is
> one question, does this work over the internet, or just local ? Info sent
> could be the uuid, so that client browser could decide to bridge the call,
> send to IVR or transfer ...
> Please give me some guide line how to set this up.
> I added to dialplan/default.xml
> -----
>
>  <extension name="socketTest">
>       <condition field="destination_number" expression="^444$">
>         <action application="socket" data="127.0.0.1:9090 async full"/>
>       </condition>
>  </extension>
>
> -----
>
>
> got the php sample file:
>
> <?php
>
> /**
>  * Based loosely on the NET_Server code in PEAR.
>  * This is only an example - considerable additional work is needed
>  * Specifically, the code in the handleConnection method should be
>  * handled in a subclass
>  *
>  *
>  */
>
> class Message
> {
>   var $properties = array();
>   var $content = null;
> }
>
> class EventSocketListener
> {
>
>   var $host;
>   var $port;
>   var $sock;
>   var $is_parent = true;
>   var $clientInfo;
>   var $clientFD;
>
>   var $connectionContext = array();
>
>
>   function &create($port, $host = "localhost")
>       {
> 	$esl = new EventSocketListener;
> 	$esl->port = $port;
> 	$esl->host = $host;
>         if (!function_exists('socket_create')) {
>             return PEAR::raiseError('Sockets extension not available.');
>         }
> 	return $esl;
>       }
>
>   function start()
>   {
> 	if (($this->sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) === false) {
> 	  echo "socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "\n";
> 	}
>
> 	if (!socket_set_option($this->sock, SOL_SOCKET, SO_REUSEADDR, 1)) {
> 	  echo 'Unable to set option on socket: '. socket_strerror(socket_last_error()) . PHP_EOL;
> 	}
>
> 	if (socket_bind($this->sock, $this->host, $this->port) === false) {
> 	  echo "socket_bind() failed: reason: " . socket_strerror(socket_last_error($this->sock)) . "\n";
> 	}
>
> 	if (socket_listen($this->sock, 5) === false) {
> 	  echo "socket_listen() failed: reason: " . socket_strerror(socket_last_error($this->sock)) . "\n";
> 	}
>
>         // Dear children, please do not become zombies
>         pcntl_signal(SIGCHLD, SIG_IGN);
>
>         // wait for incmoning connections
>         while (true)
>         {
>             // new connection
>             if(($fd = socket_accept($this->sock)))
>             {
> 	      $pid = pcntl_fork();
> 	      if($pid == -1) {
> 		  return  PEAR::raiseError('Could not fork child process.');
>                 }
>                 // This is the child => handle the request
>                 elseif($pid == 0) {
>                     // this is not the parent
>                     $this->_isParent = false;
>                     // store the new file descriptor
>                     $this->clientFD = $fd;
>
>                     $peer_host    =    "";
>                     $peer_port    =    "";
>                     socket_getpeername($this->clientFD, $peer_host, $peer_port);
>                     $this->clientInfo    =    array(
>                                                       "host"        =>    $peer_host,
>                                                       "port"        =>    $peer_port,
>                                                       "connectOn"   =>    time()
>                                                    );
> 		    $this->handleConnection();
> 		    socket_shutdown($this->clientFD, 2);
> 		    socket_close($this->clientFD);
> 		}
> 		else /* Parent does nothing */
> 		  {
> 		  }
> 	    }
> 	}
>   }
>
>   function handleConnection()
>   {
>     $fd = $this->clientFD;
>     //first, read headers & setup a state for this connection
>     $line = "";
>     socket_write($fd, "CONNECT\n\n");
>     do
>       {
> 	$line = socket_read($fd, 2048, PHP_NORMAL_READ);
> 	if (trim($line) == "")
> 	  break;
> 	//we got a header, we need to add it to the
> 	list($key, $value) = explode(":", $line);
> 	$key = trim($key);
> 	$value = trim(urldecode($value));
> 	$this->connectionContext[$key] = $value;
>       }
>     while ($line != "");
>
>     //    print_r($this->connectionContext);
>     $this->callConnected();
>
>     exit();
>
>   }
>
>   function processMessages($returnOnReply = false)
>   {
>     $fd = $this->clientFD;
>     $result = new Message();
>     $props = array();
>     while (true)
>       {
> 	do
> 	  {
> 	    $line = @socket_read($fd, 2048, PHP_NORMAL_READ);
> 	    if (socket_last_error($fd) == 104)
> 	      return null;
> 	    if ($line == null || $line == FALSE || trim($line) == "")
> 	      break;
> 	    //we got a header, we need to add it to the message
> 	    list($key, $value) = explode(":", $line);
> 	    $key = trim($key);
> 	    $value = trim(urldecode($value));
> 	    $props[$key] = $value;
> 	  }
> 	while ($line != "");
> 	$result->properties = $props;
>
> 	if (isset($props['Content-Length']))
> 	  {
> 	    $length = $props['Content-Length'];
> 	    print("Reading content - $length\n");
> 	    $data = socket_read($fd, $length);
> 	    $result->content = $data;
> 	  }
> 	if (isset($props['Content-Type']))
> 	  {
> 	    $type = $props['Content-Type'];
> 	    if ($returnOnReply &&
> 		($type == "command/reply" || $type == "api/response"))
> 	      {
> 		return $result;
> 	      }
> 	    else if ($type == "text/event-plain") //only plain events for now
> 	      {
> 		$this->handleEvent($result);
> 	      }
> 	  }
> 	else
> 	  {
> 	    print("UNKNOWN MESSAGE: \n");
> 	    print_r($result);
> 	  }
>       }
>   }
>
>
>
>   function invokeCommand($command)
>   {
>     //Send the command
>     print("Invoking: $command\n");
>     $this->sendCommand($command);
>     // Wait for the response
>     $result = $this->processMessages(true);
>     return $result;
>   }
>
>   function sendCommand($command)
>   {
>     $fd = $this->clientFD;
>     socket_write($fd, trim($command) . "\n\n");
>   }
>
> /*-----------------------------------------------------*/
>   /* Abstract Methods - should move to subclass*/
>
>   function callConnected()
>   {
>     print_r($this->connectionContext);
>     print("----------------\n");
>     $result = $this->invokeCommand("log DEBUG");
>     print_r($result);
>     $result = $this->invokeCommand("event plain ALL");
>     print_r($result);
>
>     $this->processMessages(false);
>     print("DONE PROCESSING MESSAGES");
>     print_r($this->connectionContext);
>   }
>
>   function handleCommandResponse($response)
>   {
>     print("Recieved Unhandled Response:\n");
>     print_r($response);
>   }
>
>   function handleEvent($event)
>   {
>     print("Recieved Unhandled Event:\n");
>     print_r($event);
>   }
> }
>
>
>
>
> // create a server that forks new processes
> $server  = &EventSocketListener::create(9090);
>
> // start the server
> $server->start();
> ?>
>
> --------
>
> ( this is the original file, not perfect sample) I tried to run this, and got error with auth.
>
> Also, I modify the even_socket_conf.xml  in autoload_configs/
>
> change listen-ip to  0.0.0.0 , port = 9090 , disable password
>
>
> What did I miss?
>
> Thankyou for your help
>
> Will
>
>
>
>
> _______________________________________________
> FreeSWITCH-users mailing list
> FreeSWITCH-users at lists.freeswitch.org<http://mc/compose?to=FreeSWITCH-users@lists.freeswitch.org>
> http://lists.freeswitch.org/mailman/listinfo/freeswitch-users
> UNSUBSCRIBE:http://lists.freeswitch.org/mailman/options/freeswitch-users
> http://www.freeswitch.org
>
>
>
> -----Inline Attachment Follows-----
>
>
> _______________________________________________
> FreeSWITCH-users mailing list
> FreeSWITCH-users at lists.freeswitch.org<http://mc/compose?to=FreeSWITCH-users@lists.freeswitch.org>
> http://lists.freeswitch.org/mailman/listinfo/freeswitch-users
> UNSUBSCRIBE:http://lists.freeswitch.org/mailman/options/freeswitch-users
> http://www.freeswitch.org
>
>
>
> _______________________________________________
> FreeSWITCH-users mailing list
> FreeSWITCH-users at lists.freeswitch.org
> http://lists.freeswitch.org/mailman/listinfo/freeswitch-users
> UNSUBSCRIBE:http://lists.freeswitch.org/mailman/options/freeswitch-users
> http://www.freeswitch.org
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.freeswitch.org/pipermail/freeswitch-users/attachments/20101029/81ec77c6/attachment-0001.html 


More information about the FreeSWITCH-users mailing list