我们如何将渠道消费者分享为api?

时间:2020-04-29 15:25:36

标签: python django websocket django-celery django-channels

我创建了一个使用共享任务实现一些文本操作的通道,该任务将响应返回到通道层。

#consumers.py

import json
import pdb
from asgiref.sync import async_to_sync
from channels.generic.websocket import AsyncWebsocketConsumer

from . import tasks

COMMANDS = {
    'help': {
        'help': 'Display help message.',
    },
    'sum': {
        'args': 2,
        'help': 'Calculate sum of two integer arguments. Example: `sum 12 32`.',
        'task': 'add'
    },
    'status': {
        'args': 1,
        'help': 'Check website status. Example: `status twitter.com`.',
        'task': 'url_status'
    },
}

class Consumer(AsyncWebsocketConsumer):
    async def receive(self, text_data):
        text_data_json = json.loads(text_data)
        message = text_data_json['message']

        # response_message = 'Please type `help` for the list of the commands.'
        message_parts = message.split()
        if message_parts:
            command = message_parts[0].lower()
            if command == 'help':
                response_message = 'List of the available commands:\n' + '\n'.join([f'{command} - {params["help"]} ' for command, params in COMMANDS.items()])
            elif command in COMMANDS:
                if len(message_parts[1:]) != COMMANDS[command]['args']:
                    response_message = f'Wrong arguments for the command `{command}`.'
                else:
                    getattr(tasks, COMMANDS[command]['task']).delay(self.channel_name, *message_parts[1:])
                    # response_message = f'Command `{command}` received.'
        response_message = message
        await self.channel_layer.send(
                self.channel_name,
                {
                    'type': 'chat_message',
                    'message': response_message
                }
            )

#tasks.py
@shared_task
def add(channel_layer, x, y):
    message = '{}+{}={}'.format(x, y, int(x) + int(y))
    async_to_sync(channel_layer.send)({"type": "chat.message", "message": message})

我想将此频道作为api共享,可以使用http请求进行访问。为此,我发表了以下看法。

views.py
@csrf_exempt
@api_view(['POST'])
def api(request):
    channel_layer = get_channel_layer()
    async_to_sync(channel_layer.send)('test_channel', {'type': 'hello'})
    ret = async_to_sync(channel_layer.receive)(channel_name)
    return JsonResponse({"msg":ret})

从视图中接收消息时,我收到的消息与发送时相同。如何在不使用模板中的WebSocket进行连接的情况下共享频道或处理传入消息?

1 个答案:

答案 0 :(得分:1)

如果您只希望POST请求发送邮件

views.py
@csrf_exempt
@api_view(['POST'])
def api(request):
    channel_layer = get_channel_layer()
    async_to_sync(channel_layer.send)('test_channel', {'type': 'chat.message'})
    return JsonResponse({"msg":"sent"})

您需要确保您已经在消费者中订阅了test_channel。并且您将需要针对该使用者chat_message的方法。

如果您想在帖子中回覆

您将无法使用channel_layer.send来执行此操作,因为这与您没有任何概念响应的扩展异步。此外,甚至可能没有您的使用者实例在运行,因为Channels仅在具有打开的Websocket连接且会溃败的情况下才创建实例。

所以我想你可以做:

创建消费者的实例并通过同步python代码向其发送消息将非常复杂。我建议您不要使用这种方法。它很复杂,肮脏,很可能会以各种意外的方式破裂

相反,我建议移动代码,以便您可以在HTTP视图和websocket视图之间共享它们,因为它们都可以调用这些功能。

>