def start(): global main_event_loop print_welcome() hy.start_hyrepl() blender.register() main_event_loop = asyncio.get_event_loop() assert (main_event_loop is not None) autils.call_soon(main_event_loop, bootstrap_js)
async def _run_client_loop(self): logger.info("Connecting via websockets to '{}'".format(self.url)) try: async with websockets.connect(self.url) as ws: logger.debug( "ws#{} client_loop: entering receive loop... {}".format( self.id, ws)) self._ws = ws await autils.get_result( self._change_ready_state(self.READY_STATE_OPEN)) done = False try: while not done: msg = await ws.recv() logger.debug( "ws#{} client_loop: got message len={}\n<< {}". format(self.id, len(msg), abbreviate_message_for_log(msg))) await autils.get_result( self._trigger_handler("onmessage", MessageEvent(msg))) if "{:type :client/stale}" in msg: logger.debug( "ws#{} stale client detected - scheduling page reload" .format(self.id)) logger.warning( "Detected stale client - will reload...") # give the whole system some time before we attempt to refresh await asyncio.sleep(5) autils.call_soon(self._main_loop, self._reload_page) done = True except websockets.exceptions.ConnectionClosed as e: logger.debug( "ws#{} client_loop: connection closed {}".format( self.id, e)) logger.warning("Websockets connection lost") self._trigger_handler("onerror", ErrorEvent(e)) except Exception as e: logger.debug("ws#{} client_loop: exception {}".format( self.id, e)) logger.error("Websocket ran into problems {}".format(e)) self._trigger_handler("onerror", ErrorEvent(e)) logger.debug("ws#{} client_loop: leaving...".format(self.id)) self._ws = None except Exception as e: self.readyState = self.READY_STATE_CLOSED await autils.get_result( self._trigger_handler("onerror", ErrorEvent(e)))
def __init__(self, url, protocols=None): assert (protocols is None) global global_next_ws_instance_id self.id = global_next_ws_instance_id global_next_ws_instance_id += 1 logger.debug("ws#{} __init__".format(self.id)) start_async_loop_if_needed() assert threading.current_thread() is threading.main_thread() self._main_loop = asyncio.get_event_loop() # noinspection PyUnresolvedReferences self._window = self.__class__.window self._ws = None self.protocol = "" self.readyState = self.READY_STATE_CONNECTING self.url = url self.onmessage = None self.onopen = None self.onclose = None self.onerror = None autils.call_soon(async_loop, self._run_client_loop)
def handle_session_message(session, msg, request): autils.call_soon(main_loop, process_session_message, session, msg, request)
def _change_ready_state(self, new_state): self.readyState = new_state autils.call_soon(self._main_loop, self._process_onreadystatechange)
def send(self, body=None, *_): logger.debug("send {}".format(body)) assert isinstance(body, str) autils.call_soon(async_loop, self._send_request, body)
def send(self, msg, *_): logger.debug("ws#{} send msg={}".format( self.id, abbreviate_message_for_log(msg))) autils.call_soon(async_loop, self._send_message, msg)
def _trigger_handler(self, handler_name, *args): return autils.call_soon(self._main_loop, self._trigger_handler_async, handler_name, *args)