Skip to content

Commit

Permalink
logging and handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex committed Dec 2, 2024
1 parent a42f32a commit bd06c4c
Showing 1 changed file with 3 additions and 6 deletions.
9 changes: 3 additions & 6 deletions python/nistoar/midas/dbio/notifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,34 +5,31 @@
import copy
import logging

logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

class Notifier:
def __init__(self, host="localhost", port=8765):
self.host = host
self.port = port
self.server = None
self.clients = set() # Initialize the clients set
self.clients = set()

async def start(self):
try:
self.server = await websockets.serve(self.websocket_handler, self.host, self.port)
logger.info(f"WebSocket server started on ws://{self.host}:{self.port}")
#logger.info(f"WebSocket server started on ws://{self.host}:{self.port}")
except Exception as e:
logger.error(f"Failed to start WebSocket server: {e}")


async def websocket_handler(self, websocket):
# Add the new client to the set
self.clients.add(websocket)
try:
async for message in websocket:
await self.send_message_to_clients(message)
except Exception as e:
logger.error(f"Error in websocket_handler: {e}")
finally:
# Remove the client from the set when they disconnect
self.clients.remove(websocket)

async def send_message_to_clients(self, message):
Expand All @@ -48,7 +45,7 @@ async def stop(self):
self.server.close()
await self.server.wait_closed()
self.server = None
logger.info("WebSocket server stopped")
#logger.info("WebSocket server stopped")

async def wait_closed(self):
if self.server:
Expand Down

0 comments on commit bd06c4c

Please sign in to comment.