async def _inner_messages( self, ws: websockets.WebSocketClientProtocol) -> AsyncIterable[str]: # Terminate the recv() loop as soon as the next message timed out, so the outer loop can reconnect. try: while True: try: raw_msg: str = await asyncio.wait_for( ws.recv(), timeout=self.MESSAGE_TIMEOUT) yield raw_msg except asyncio.TimeoutError: payload = {"op": CONSTANTS.PONG_ENDPOINT_NAME} pong_waiter = ws.send(ujson.dumps(payload)) async with self._throttler.execute_task( CONSTANTS.PONG_ENDPOINT_NAME): await asyncio.wait_for(pong_waiter, timeout=self.PING_TIMEOUT) self._last_recv_time = time.time() except asyncio.TimeoutError: self.logger().warning( "WebSocket ping timed out. Going to reconnect...") return except websockets.ConnectionClosed: return finally: await ws.close()
async def _inner_messages( self, ws: websockets.WebSocketClientProtocol) -> AsyncIterable[str]: # Terminate the recv() loop as soon as the next message timed out, so the outer loop can reconnect. try: while True: try: raw_msg: str = await asyncio.wait_for( ws.recv(), timeout=self.MESSAGE_TIMEOUT) yield raw_msg except asyncio.TimeoutError: try: pong_waiter = ws.send(ujson.dumps(PONG_PAYLOAD)) await asyncio.wait_for(pong_waiter, timeout=self.PING_TIMEOUT) self._last_recv_time = time.time() except asyncio.TimeoutError: raise except asyncio.TimeoutError: self.logger().warning( "WebSocket ping timed out. Going to reconnect...") return except websockets.ConnectionClosed: return finally: await ws.close()
def handshake(ws: websockets.WebSocketClientProtocol) -> Coroutine: """handshake send a handshake to client's websocket""" handshake_message = { 'data': { 'app': 'photoshop', 'id': 'a8dcd02f', 'version': '6.3.5' } } return ws.send(json.dumps(handshake_message))
def _schedule_websocket_send(self, websocket: websockets.WebSocketClientProtocol, endpoint: OpenSongEndpoint, delay: int = 0, add_pending_request: bool = True): if add_pending_request: self._add_pending_request(endpoint) send_future = lambda: asyncio.ensure_future( websocket.send(endpoint.url)) asyncio.get_event_loop().call_later(delay, send_future) return True
def send_replace(ws: websockets.WebSocketClientProtocol, filepaths: List[str]) -> Coroutine: """send_replace will format a messages to replace a file and send the message through websocket""" file_import_message = { 'data': { 'command': 'DEFAULT_ACTION', 'action': 'REPLACE_CURRENT', 'data': { 'artwork': filepaths, 'origin': 'Blender' } } } return ws.send(json.dumps(file_import_message))
def send_files(ws: websockets.WebSocketClientProtocol, filepaths: List[str]) -> Coroutine: """send_files will format a messages to import files and send the message through websocket""" file_import_message = { 'data': { 'command': 'DEFAULT_ACTION', 'action': 'IMPORT_CURRENT', 'data': { 'paths': filepaths, 'origin': 'Blender' } } } return ws.send(json.dumps(file_import_message))
async def keep_alive(socket: websockets.WebSocketClientProtocol): while True: ping = input('{"op":"ping"}') socket.send(ping) await asyncio.sleep(3600)