29 lines
1 KiB
Python
29 lines
1 KiB
Python
from channels.layers import get_channel_layer
|
|
|
|
|
|
class NotificationDispatcher:
|
|
channel_layer = None
|
|
room_group_name = "general"
|
|
|
|
def __init__(self):
|
|
self.channel_layer = get_channel_layer('default')
|
|
if not self.channel_layer:
|
|
raise Exception("Could not get channel layer")
|
|
|
|
async def run_forever(self):
|
|
# Infinite loop to continuously listen for messages
|
|
print("Listening for messages...")
|
|
channel_name = await self.channel_layer.new_channel()
|
|
await self.channel_layer.group_add(self.room_group_name, channel_name)
|
|
print("Channel name:", channel_name)
|
|
while True:
|
|
# Blocking receive to get the message from the channel layer
|
|
message = await self.channel_layer.receive(channel_name)
|
|
|
|
if message:
|
|
# Process the received message
|
|
print("Received message:", message)
|
|
await self.dispatch(message)
|
|
|
|
async def dispatch(self, message):
|
|
print("Dispatching message:", message)
|