Django channel group_send all messages to one channel in the group
Я пытаюсь сделать веб-приложение для чата в реальном времени с каналом Django. Когда я пытаюсь использовать Postman, я создаю несколько WS соединений с ним. Я столкнулся со странной вещью, допустим, у меня есть 2 соединения с одним и тем же сервером на порту 8000. Затем я отправляю сообщение через group_send, в идеале каждое соединение получит по 1 сообщению, но получается, что новому соединению отправляется 2 сообщения.
Вот мой потребитель:
class RealtimeConsumer(AsyncJsonWebsocketConsumer):
async def websocket_connect(self, event):
user_id = self.scope['user']
await self.channel_layer.group_add(
str(user_id), # use id as the group name
self.channel_name
)
await self.accept()
await self.send_json(
{
"connectionKey": self.channel_name,
"clientId": str(user_id),
},
)
async def send_message(self, event):
message = event["message"]
await self.send_json(
{
"messages": message,
"connectionKey": self.channel_name,
},
)
async def websocket_disconnect(self, message):
user_id = self.scope['user']
await self.channel_layer.group_discard(
str(user_id), # group name
self.channel_name
)
Отправить в группу func:
def send_single_user_msg(channel_name, message):
channel_layer = get_channel_layer()
async_to_sync(channel_layer.send)(
channel_name, {
'type': 'send_message',
"text": message,
}
)
Я использую локальный сервер redis в качестве канального уровня.
Любая идея будет очень признательна.