我们如何将频道使用者作为api共享?

问题描述 投票:0回答:1

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

#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进行连接的情况下共享频道或处理传入消息?

python django websocket django-celery django-channels
1个回答
0
投票

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

所以我想你可以做:

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

相反,我建议移动代码,您想在HTTP视图和websocket视图之间共享一个地方(而不是使用者的一部分),因为它们都可以调用这些函数。

© www.soinside.com 2019 - 2024. All rights reserved.