首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何从django视图向消费者发送消息(django-channels)?

如何从django视图向消费者发送消息(django-channels)?
EN

Stack Overflow用户
提问于 2020-01-28 15:22:20
回答 1查看 674关注 0票数 3

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

代码语言:javascript
复制
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视图是这样的:

代码语言:javascript
复制
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>')

URL url类似于:

代码语言:javascript
复制
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

EN

回答 1

Stack Overflow用户

发布于 2020-01-28 16:40:38

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

代码语言:javascript
复制
async_to_sync(channel_layer.group_send)(
    'kafka',
    {
        'type': 'kafka.message',
        'message': 'Test message'
    }
)

所有包含已更正代码的视图代码:

代码语言:javascript
复制
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>')
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59943869

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档