def connect(self): """Establish a connection to the chat server. Returns when an error has occurred, or :func:`disconnect` has been called. """ self._session = aiohttp.ClientSession(cookies=self._cookies) try: self._channel = channel.Channel( self._cookies, self._session, max_retries=self._max_retries, retry_backoff_base=self._retry_backoff_base ) # Forward the Channel events to the Client events. self._channel.on_connect.add_observer(self.on_connect.fire) self._channel.on_reconnect.add_observer(self.on_reconnect.fire) self._channel.on_disconnect.add_observer(self.on_disconnect.fire) self._channel.on_receive_array.add_observer(self._on_receive_array) # Wrap the coroutine in a Future so it can be cancelled. self._listen_future = asyncio.async(self._channel.listen()) # Listen for StateUpdate messages from the Channel until it # disconnects. try: yield from self._listen_future except asyncio.CancelledError: pass logger.info( 'Client.connect returning because Channel.listen returned' ) finally: self._session.close()
def connect(self): """Establish a connection to the chat server. Returns when an error has occurred, or :func:`disconnect` has been called. """ proxy = os.environ.get('HTTP_PROXY') self._session = http_utils.Session(self._cookies, proxy=proxy) try: self._channel = channel.Channel(self._session, self._max_retries, self._retry_backoff_base) # Forward the Channel events to the Client events. self._channel.on_connect.add_observer(self.on_connect.fire) self._channel.on_reconnect.add_observer(self.on_reconnect.fire) self._channel.on_disconnect.add_observer(self.on_disconnect.fire) self._channel.on_receive_array.add_observer(self._on_receive_array) # Wrap the coroutine in a Future so it can be cancelled. self._listen_future = asyncio. async (self._channel.listen()) # Listen for StateUpdate messages from the Channel until it # disconnects. try: yield from self._listen_future except asyncio.CancelledError: # If this task is cancelled, we need to cancel our child task # as well. We don't need an additional yield because listen # cancels immediately. self._listen_future.cancel() logger.info( 'Client.connect returning because Channel.listen returned') finally: self._session.close()
def connect(self): """Establish a connection to the chat server. Returns when an error has occurred, or Client.disconnect has been called. """ initial_data = yield from self._initialize_chat() self._channel = channel.Channel(self._cookies, self._channel_path, self._clid, self._channel_ec_param, self._channel_prop_param, self._connector) @asyncio.coroutine def _on_connect(): """Wrapper to fire on_connect with initial_data.""" yield from self.on_connect.fire(initial_data) self._channel.on_connect.add_observer(_on_connect) self._channel.on_reconnect.add_observer(self.on_reconnect.fire) self._channel.on_disconnect.add_observer(self.on_disconnect.fire) self._channel.on_message.add_observer(self._on_push_data) self._listen_future = asyncio. async (self._channel.listen()) try: yield from self._listen_future except asyncio.CancelledError: pass logger.info('disconnecting gracefully')
def __init__(self, cookies): """Create new client. cookies is a dictionary of authentication cookies. """ # Event fired when the client connects for the first time with # arguments (). self.on_connect = event.Event('Client.on_connect') # Event fired when the client reconnects after being disconnected with # arguments (). self.on_reconnect = event.Event('Client.on_reconnect') # Event fired when the client is disconnected with arguments (). self.on_disconnect = event.Event('Client.on_disconnect') # Event fired when a StateUpdate arrives with arguments (state_update). self.on_state_update = event.Event('Client.on_state_update') self._cookies = cookies proxy = os.environ.get('HTTP_PROXY') if proxy: self._connector = aiohttp.ProxyConnector(proxy) else: self._connector = aiohttp.TCPConnector() self._channel = channel.Channel(self._cookies, self._connector) # Future for Channel.listen self._listen_future = None self._request_header = hangouts_pb2.RequestHeader( # Ignore most of the RequestHeader fields since they aren't # required. Sending a recognized client_id is important because it # changes the behaviour of some APIs (eg. get_conversation will # filter out EVENT_TYPE_GROUP_LINK_SHARING_MODIFICATION without # it). client_version=hangouts_pb2.ClientVersion( client_id=hangouts_pb2.CLIENT_ID_WEB_HANGOUTS, major_version='hangups-{}'.format(version.__version__), ), language_code='en', ) # String identifying this client (populated later): self._client_id = None # String email address for this account (populated later): self._email = None # Active client management parameters: # Time in seconds that the client as last set as active: self._last_active_secs = 0.0 # ActiveClientState enum int value or None: self._active_client_state = None
def connect(self): """Connect to the server and receive events.""" initial_data = yield from self._initialize_chat() self._channel = channel.Channel(self._cookies, self._channel_path, self._clid, self._channel_ec_param, self._channel_prop_param, self._connector) self._channel.on_connect.add_observer( lambda: self.__on_pre_connect.fire(initial_data)) self._channel.on_reconnect.add_observer(self.on_reconnect.fire) self._channel.on_disconnect.add_observer(self.on_disconnect.fire) self._channel.on_message.add_observer(self._on_push_data) yield from self._channel.listen()
def __init__(self, cookies): """Create new client. cookies is a dictionary of authentication cookies. """ # Event fired when the client connects for the first time with # arguments (). self.on_connect = event.Event('Client.on_connect') # Event fired when the client reconnects after being disconnected with # arguments (). self.on_reconnect = event.Event('Client.on_reconnect') # Event fired when the client is disconnected with arguments (). self.on_disconnect = event.Event('Client.on_disconnect') # Event fired when a StateUpdate arrives with arguments (state_update). self.on_state_update = event.Event('Client.on_state_update') self._cookies = cookies proxy = os.environ.get('HTTP_PROXY') if proxy: self._connector = aiohttp.ProxyConnector(proxy) else: self._connector = aiohttp.TCPConnector() self._channel = channel.Channel(self._cookies, self._connector) # Future for Channel.listen self._listen_future = None self._request_header = hangouts_pb2.RequestHeader( # Ignore most of the RequestHeader fields since they aren't # required. client_version=hangouts_pb2.ClientVersion( major_version='hangups-{}'.format(__version__), ), language_code='en', ) # String identifying this client (populated later): self._client_id = None # String email address for this account (populated later): self._email = None # Active client management parameters: # Time in seconds that the client as last set as active: self._last_active_secs = 0.0 # ActiveClientState enum int value or None: self._active_client_state = None