Run one websocket tx handler for all clients, rather than one each.

This commit is contained in:
Matthew Stratford 2021-04-11 20:29:16 +01:00
parent 2b138607fd
commit ecbab09f61

View file

@ -37,6 +37,7 @@ class WebsocketServer:
) )
asyncio.get_event_loop().run_until_complete(self.websocket_server) asyncio.get_event_loop().run_until_complete(self.websocket_server)
asyncio.get_event_loop().run_until_complete(self.handle_to_webstudio())
try: try:
asyncio.get_event_loop().run_forever() asyncio.get_event_loop().run_forever()
@ -45,6 +46,7 @@ class WebsocketServer:
self.quit() self.quit()
def quit(self): def quit(self):
self.logger.log.info("Quitting.")
del self.websocket_server del self.websocket_server
del self.logger del self.logger
_exit(0) _exit(0)
@ -63,15 +65,13 @@ class WebsocketServer:
channel.put("WEBSOCKET:STATUS") channel.put("WEBSOCKET:STATUS")
self.from_webstudio = asyncio.create_task(self.handle_from_webstudio(websocket)) self.from_webstudio = asyncio.create_task(self.handle_from_webstudio(websocket))
self.to_webstudio = asyncio.create_task(self.handle_to_webstudio())
try: try:
self.threads = await shield( self.threads = await shield(
asyncio.gather(self.from_webstudio, self.to_webstudio) asyncio.gather(self.from_webstudio)
) )
finally: finally:
self.from_webstudio.cancel() self.from_webstudio.cancel()
self.to_webstudio.cancel()
async def handle_from_webstudio(self, websocket): async def handle_from_webstudio(self, websocket):
try: try:
@ -187,10 +187,9 @@ class WebsocketServer:
try: try:
message = self.webstudio_to_q[channel].get_nowait() message = self.webstudio_to_q[channel].get_nowait()
source = message.split(":")[0] source = message.split(":")[0]
# TODO ENUM # TODO ENUM
if source not in ["WEBSOCKET", "ALL"]: if source not in ["WEBSOCKET", "ALL"]:
print( self.logger.log.error(
"ERROR: Message received from invalid source to websocket_handler. Ignored.", "ERROR: Message received from invalid source to websocket_handler. Ignored.",
source, source,
message, message,