[Freeswitch-users] Re- End Lua script after HangupHook handled without all the extra code to handle the return to the function

Abaci B abaci64 at gmail.com
Fri Feb 26 17:29:17 MSK 2016


See
https://freeswitch.org/confluence/display/FREESWITCH/Lua+API+Reference#LuaAPIReference-session:setHangupHook
for a few ways to exit the lua script (error(), return "exit", return
"die", s:destroy("error message")). I personally tried return "exit" but it
seems to me that it only exits the calling function, haven't had a chance
to look further, it's possible that the calling it from the within a
function is different. if you play around and figure out please report back.

On Thu, Feb 25, 2016 at 9:33 PM, Andrew Keil <andrew.keil at visytel.com>
wrote:

> To FreeSWITCH Users,
>
>
>
> See below for a sample template for a Lua Service Script running inside
> FreeSWITCH.
>
>
>
> The issue I have is fairly straightforward.
>
>
>
> I need a function to run when hangup is detected (ie. at the end of the
> call) however I understand this must not delay ending the script.  This
> function is CleanUp().  Then I would like the service to end.
>
>
>
> The problem I am having is if the caller hangs up during the playback of
> “intro.wav” (as shown inside the MainService() function below), then the
> code jumps to the myHangupHook which calls CleanUp() perfectly, the issue
> is once CleanUp() is complete I would like the Lua script to end there and
> then (ie. at the bottom of CleanUp()).  What actually happens is it returns
> to MainService() and continues to try and play “info.wav”, unless I either
> check for session:ready() everywhere or add a goto as shown below under
> each streamFile() function call.
>
>
>
> My aim is to reduce extra code and to make the Lua script simpler and
> easier to read.  Also I would like to try and avoid goto statements, which
> I know can be done with if (session:ready()) etc….
>
>
>
> So is there a way to stop a Lua script running inside FreeSWITCH cleanly?
> I have tried the os.exit() this is barred from use by FreeSWITCH.  I have
> also tried session:destroy() which crashes FreeSWITCH (version 1.6.5 on
> CentOS 6.7, CentOS 7 and windows) 100% of the time!
>
>
>
> I could look further into the Lua additions done by the FreeSWITCH team in
> the source code, however if someone has already solved this then that would
> be the best solution.
>
>
>
> FYI: Obviously the script below is simple, however I am sure that you
> understand if the script was complicated having to use “*if
> (session:ready()) then ….”* or “*if (not session:ready()) then goto
> HANGUPEXIT end”* makes the code ugly.
>
>
>
> Thanks in advance,
>
>
>
> Andrew Keil
>
> *Visytel Pty Ltd*
>
>
>
>
>
>
>
> ------------------------------------------------------------------------------
> Sample Lua Service
> -----------------------------------------------------------------------
>
>
>
> -- Lua template for FreeSWITCH service
>
> -- By: Andrew Keil (Visytel Pty Ltd)
>
> -- Email: support at visytel.com
>
>
>
> -- Setup script wide variables here
>
>
>
> function PreAnswer()
>
>                 freeswitch.consoleLog("INFO", "PRE ANSWER SECTION\n");
>
>                 -- Add your pre answer code from here
>
>
>
>                 -- End of your pre answer code
>
>                 freeswitch.consoleLog("INFO", "PRE ANSWER SECTION
> COMPLETE\n");
>
> end
>
>
>
> function AnswerCaller()
>
>                 session:answer()
>
>                 session:sleep(1000)
>
> end
>
>
>
> function MainService()
>
>                 freeswitch.consoleLog("INFO", "MAIN SERVICE
> SECTION\n");
>
>                 if (session:ready()) then
>
>                                 -- Note (1): If you wish to end the call
> then simply use: goto ENDSERVICE
>
>                                 -- Note (2): To terminate the service
> sooner when HANGUP is detected use: if (not session:ready()) then goto
> HANGUPEXIT end
>
>                                 -- Add your main service code from
> here               (caller would have been answered)
>
>
>
>                                 session:streamFile("intro.wav")
>
>                                 if (not session:ready()) then goto
> HANGUPEXIT end
>
>                                 session:streamFile("info.wav")
>
>                                 if (not session:ready()) then goto
> HANGUPEXIT end
>
>
> session:streamFile("outro.wav")
>
>                                 if (not session:ready()) then goto
> HANGUPEXIT end
>
>
>
>                                 -- End of your main service code
>
>                 end
>
>                 ::ENDSERVICE::
>
>                 if (session:ready()) then
>
>                                 -- End of service so hangup
>
>                                 session:hangup()  -- Should automatically
> jump to CleanUp() via hangup handler if caller still online at this stage
>
>                 end
>
>                 goto END
>
>                 ::HANGUPEXIT::
>
>                 freeswitch.consoleLog("INFO", "END OF SERVICE (HANGUP
> DETECTED)\n");
>
>                 ::END::
>
>                 freeswitch.consoleLog("INFO", "MAIN SERVICE SECTION
> COMPLETE\n");
>
> end
>
>
>
> function CleanUp()
>
>                 freeswitch.consoleLog("INFO", "CLEANUP SECTION\n");
>
>                 -- Add your cleanup code from here (caller would have been
> disconnected)
>
>
>
>                 -- End of your cleanup code
>
>                 freeswitch.consoleLog("INFO", "CLEANUP SECTION
> COMPLETE\n");
>
> end
>
>
>
> function myHangupHook(s, status, arg)
>
>                 session:hangup()
>
>                 CleanUp() -- Run CleanUp function now since the caller has
> disconnected
>
> end
>
>
>
> -- Setup Hangup event handler here
>
> v_hangup = "HANGUP"
>
> session:setHangupHook("myHangupHook", "v_hangup")
>
>
>
> -- Call service functions in order
>
> PreAnswer()
>
> AnswerCaller()
>
> MainService()
>
> -- End of Lua service
>
>
>
>
>
> _________________________________________________________________________
> Professional FreeSWITCH Consulting Services:
> consulting at freeswitch.org
> http://www.freeswitchsolutions.com
>
> Official FreeSWITCH Sites
> http://www.freeswitch.org
> http://confluence.freeswitch.org
> http://www.cluecon.com
>
> 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/20160226/c27bd9fb/attachment-0001.html 


Join us at ClueCon 2016 Aug 8-12, 2016
More information about the FreeSWITCH-users mailing list