【问题标题】:How to send message from django view to consumer (django-channels)?如何从 django 视图向消费者(django-channels)发送消息?
【发布时间】:2020-05-13 13:55:40
【问题描述】:

我想从 Django 视图向 django 频道消费者发送一些消息。我有这样的消费者:

from channels.generic.websocket import AsyncWebsocketConsumer
import json

class KafkaConsumer(AsyncWebsocketConsumer):
    async def connect(self):
        self.room_group_name = 'kafka'

        # Join room group
        await self.channel_layer.group_add(
            self.room_group_name,
            self.channel_name
        )

        await self.accept()

    async def disconnect(self, close_code):
        # Leave room group
        await self.channel_layer.group_discard(
            self.room_group_name,
            self.channel_name
        )

    # Receive message from WebSocket
    async def receive(self, text_data):
        text_data_json = json.loads(text_data)
        message = text_data_json['message']

        # Send message to room group
        await self.channel_layer.group_send(
            self.room_group_name,
            {
                'type': 'kafka_message',
                'message': message
            }
        )

    # Receive message from room group
    async def kafka_message(self, event):
        message = event['message']

        # Send message to WebSocket
        await self.send(text_data=json.dumps({
            'message': message
        }))

而且,我的 Django 视图是这样的:

from django.views.generic import TemplateView
from django.http import HttpResponse

from channels.layers import get_channel_layer
from asgiref.sync import async_to_sync


class LogView(TemplateView):
    template_name = "kafka/index.html"


def testview(request):
    channel_layer = get_channel_layer()

    async_to_sync(channel_layer.group_send(
        'kafka',
        {
            'type': 'kafka.message',
            'message': 'Test message'
        }
    ))
    return HttpResponse('<p>Done</p>')

网址网址是这样的:

from django.urls import path
from .views import LogView, testview

urlpatterns = [
    path(r'', LogView.as_view()),
    path(r'test/', testview),

]

所以,当我执行http://mydevhost/test/ 时,消费者不会收到消息。但是,我可以从消费者/消费者内部发送消息,即渠道消费者中的KafkaConsumer.receive

【问题讨论】:

    标签: django django-channels


    【解决方案1】:

    async_to_sync 上的错误相当愚蠢。实际上async_to_sync 应该只包装channel_layer.group_send 而不是整个,即async_to_sync(channel_layer.group_send)。所以调用看起来像:

    async_to_sync(channel_layer.group_send)(
        'kafka',
        {
            'type': 'kafka.message',
            'message': 'Test message'
        }
    )
    

    所有查看代码更正后的代码:

    from django.views.generic import TemplateView
    from django.http import HttpResponse
    
    from channels.layers import get_channel_layer
    from asgiref.sync import async_to_sync
    
    
    class LogView(TemplateView):
        template_name = "kafka/index.html"
    
    
    def testview(request):
        channel_layer = get_channel_layer()
    
        async_to_sync(channel_layer.group_send)(
            'kafka',
            {
                'type': 'kafka.message',
                'message': 'Test message'
            }
        )
        return HttpResponse('<p>Done</p>')
    

    【讨论】:

    • 谢谢!对此感到疯狂......犯了同样的错误哈哈
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-06-09
    • 2023-03-17
    • 1970-01-01
    • 2018-07-29
    • 2018-07-11
    • 2020-08-15
    • 1970-01-01
    相关资源
    最近更新 更多