added the hability to control fbchat from an external event loop

This commit is contained in:
Thiago
2017-04-04 11:14:12 -03:00
parent 62ce49bd17
commit 2c4b9fbf80

View File

@@ -74,6 +74,7 @@ class Client(object):
raise Exception("id and password or config is needed") raise Exception("id and password or config is needed")
self.debug = debug self.debug = debug
self.sticky, self.pool = (None, None)
self._session = requests.session() self._session = requests.session()
self.req_counter = 1 self.req_counter = 1
self.seq = "0" self.seq = "0"
@@ -776,23 +777,44 @@ class Client(object):
self.on_message_error(sys.exc_info(), m) self.on_message_error(sys.exc_info(), m)
def listen(self, markAlive=True): def start_listening(self):
"""Start listening from an external event loop."""
self.listening = True self.listening = True
sticky, pool = self._getSticky() self.sticky, self.pool = self._getSticky()
def do_one_listen(self, markAlive=True):
"""Does one cycle of the listening loop.
This method is only useful if you want to control fbchat from an
external event loop."""
try:
if markAlive: self.ping(self.sticky)
try:
content = self._pullMessage(self.sticky, self.pool)
if content: self._parseMessage(content)
except requests.exceptions.RequestException as e:
pass
except KeyboardInterrupt:
self.listening = False
except requests.exceptions.Timeout:
pass
def stop_listening(self):
"""Cleans up the variables from start_listening."""
self.listening = False
self.sticky, self.pool = (None, None)
def listen(self, markAlive=True):
self.start_listening()
log.info("Listening...") log.info("Listening...")
while self.listening: while self.listening:
try: self.do_one_listen(markAlive)
if markAlive: self.ping(sticky)
try: self.stop_listening()
content = self._pullMessage(sticky, pool)
if content: self._parseMessage(content)
except requests.exceptions.RequestException as e:
continue
except KeyboardInterrupt:
break
except requests.exceptions.Timeout:
pass
def getUserInfo(self, *user_ids): def getUserInfo(self, *user_ids):
"""Get user info from id. Unordered. """Get user info from id. Unordered.