2024-04-25 21:45:57 +00:00
|
|
|
from channels.layers import get_channel_layer
|
|
|
|
|
|
|
|
|
|
|
|
class NotificationDispatcher:
|
|
|
|
channel_layer = None
|
2024-04-25 23:13:18 +00:00
|
|
|
channel_name = None
|
2024-04-25 21:45:57 +00:00
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
self.channel_layer = get_channel_layer()
|
2024-04-25 22:56:12 +00:00
|
|
|
if not self.channel_layer:
|
|
|
|
raise Exception("Could not get channel layer")
|
2024-04-25 21:45:57 +00:00
|
|
|
|
|
|
|
async def run_forever(self):
|
|
|
|
# Infinite loop to continuously listen for messages
|
2024-04-25 22:31:34 +00:00
|
|
|
print("Listening for messages...")
|
2024-04-25 23:13:18 +00:00
|
|
|
self.channel_name = await self.channel_layer.new_channel()
|
|
|
|
print("Channel name:", self.channel_name)
|
2024-04-25 21:45:57 +00:00
|
|
|
while True:
|
|
|
|
# Blocking receive to get the message from the channel layer
|
2024-04-25 23:13:18 +00:00
|
|
|
message = await self.channel_layer.receive(self.channel_name)
|
2024-04-25 21:45:57 +00:00
|
|
|
|
|
|
|
if message:
|
|
|
|
# Process the received message
|
|
|
|
print("Received message:", message)
|
|
|
|
await self.dispatch(message)
|
2024-04-25 22:56:12 +00:00
|
|
|
|
|
|
|
async def dispatch(self, message):
|
|
|
|
print("Dispatching message:", message)
|