Skip to content
Snippets Groups Projects
Commit d414a68c authored by Mario Raciti's avatar Mario Raciti
Browse files

TMSS-413: Add asyncio event loop to run websocket server as a thread

parent e66c7cac
No related branches found
No related tags found
1 merge request!282Resolve TMSS-417
......@@ -32,6 +32,7 @@ from lofar.sas.tmss.client.tmssbuslistener import *
import asyncio
import socketio
from aiohttp import web
from threading import Thread
class WebsocketServer():
......@@ -45,11 +46,11 @@ class WebsocketServer():
self.app = web.Application()
self.sio.attach(self.app)
def start(self):
def start(self, loop): # Start a websocket server
asyncio.set_event_loop(loop) # FIXME: set_wakeup_fd only works in main thread.
web.run_app(self.app, host='127.0.0.1', port=5678)
def stop(self):
# TODO:
def stop(self): # TODO: Gracefully shutdown the server
# self.app.shutdown()
# self.app.cleanup()
pass
......@@ -61,7 +62,7 @@ class WebsocketServer():
@staticmethod
@sio.event
async def message(sid, data):
async def message(sid, data): # Just for debugging
logger.info('Received: %s' % data)
await WebsocketServer.sio.emit('broadcastNotify', {'msg': 'Broadcast notify.'})
......@@ -82,11 +83,18 @@ class TMSSEventMessageHandlerForWebsocket(TMSSEventMessageHandler):
'''
'''
def __init__(self) -> None:
self._ws = WebsocketServer() # TODO: create websocket
self._ws = WebsocketServer()
super().__init__()
def start_handling(self):
self._ws.start() # TODO: Run the server in a separated thread
# self._ws.start() # TODO: Run the server in a separated thread
# FIXME:
# If include lines 94-95 -> ValueError: set_wakeup_fd only works in main thread.
# If comment lines 94-95 and run _ws.start() without loop -> RuntimeError: There is no current event loop in thread 'Thread-1'.
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
self._thread = Thread(target=self._ws.start, args=(loop,), daemon=True)
self._thread.start()
super().start_handling()
def stop_handling(self):
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment