EvoluxBR / greenswitch

Battle proven FreeSWITCH Event Socket Protocol client implementation with Gevent
Other
126 stars 50 forks source link

Equivalent code #41

Closed Otoru closed 5 years ago

Otoru commented 5 years ago

For not knowing where to look for examples, I could not build an equivalent of the code below using greeswitch and I need help.

Code:

import ESL

con = ESL.ESLconnection('localhost', '8021', 'ClueCon')

if con.connected():
    con.events('plain', 'all')
    while 1:
        e = con.recvEvent()
        if e:
            print e.serialize()
diasbruno commented 5 years ago

Hi @Otoru,

greenswitch allows you to register handlers for any event that freeswitch sends.

Here is one example for handling the HEARTBEAT event:

import gevent
from greenswitch import InboundESL

con = InboundESL('127.0.0.1', 8021, 'ClueCon')

# handling an event 
con.register_handle('HEARTBEAT', lambda event: print('heartbeating', event))

con.connect()

event = con.send('event plain all')

if con.connected:
  while True:
    try:
      gevent.sleep(1) 
    except KeyboardInterrupt:
      con.stop()
      break
diasbruno commented 5 years ago

In case you meant "use greenswitch to wait for the next event", I believe that's not possible.

Otoru commented 5 years ago

The code above meets my need very well. A question for what is the gevent.sleep(1) statement in this code?

diasbruno commented 5 years ago

The sleep is to allow other resources "do some work".

Otoru commented 5 years ago

Thank you for your help. If new doubts arise I publish again.