This commit is contained in:
j3d1 2024-04-26 01:22:08 +02:00
parent 186392a801
commit e2bdeedfe3

View file

@ -3,21 +3,22 @@ from channels.layers import get_channel_layer
class NotificationDispatcher: class NotificationDispatcher:
channel_layer = None channel_layer = None
channel_name = None room_group_name = "general"
def __init__(self): def __init__(self):
self.channel_layer = get_channel_layer() self.channel_layer = get_channel_layer('default')
if not self.channel_layer: if not self.channel_layer:
raise Exception("Could not get channel layer") raise Exception("Could not get channel layer")
async def run_forever(self): async def run_forever(self):
# Infinite loop to continuously listen for messages # Infinite loop to continuously listen for messages
print("Listening for messages...") print("Listening for messages...")
self.channel_name = await self.channel_layer.new_channel() channel_name = await self.channel_layer.new_channel()
print("Channel name:", self.channel_name) await self.channel_layer.group_add(self.room_group_name, channel_name)
print("Channel name:", channel_name)
while True: while True:
# Blocking receive to get the message from the channel layer # Blocking receive to get the message from the channel layer
message = await self.channel_layer.receive(self.channel_name) message = await self.channel_layer.receive(channel_name)
if message: if message:
# Process the received message # Process the received message