AsyncWebsocketConsumer VS 异步消费者

AsyncWebsocketConsumer VS AsyncConsumer

我正在尝试在我的项目中使用 Channels2。这是我第一次在 Django 中遇到频道 :) 我这里有两个主要有用且几乎完整的资源:1) youtube 上的视频 DJANGO CHANNELS 2 Tutorial (V2) - Real Time 2)document of Channel in Read The Doc

因为我不知道我的代码将来会发生什么,所以我需要你帮我选择使用源代码 #1 中提到的 AsyncConsumer 还是使用的 AsyncWebsocketConsumer在源代码 # 2 中用于启动 Django channel app 以这种方式包括:

from channels.generic.websocket import AsyncWebsocketConsumer
from channels.consumer import AsyncConsumer

解释:

class AsyncConsumer:
    """
    Base consumer class. Implements the ASGI application spec, and adds on
    channel layer management and routing of events to named methods based
    on their type.
    """

class AsyncWebsocketConsumer(AsyncConsumer):
    """
    Base WebSocket consumer, async version. Provides a general encapsulation
    for the WebSocket handling model that other applications can build on.
    """

我使用频道的目标:尝试集成实时聊天,notification/alert/transfer_data针对特定情况向客户提供。 (现在应用程序在没有 Websocket 和 DRF 的情况下工作)

如果您有任何建议、想法或通知,我将非常高兴listen.thank。

Channels 是一个旨在用于处理不同协议的项目,包括但不限于 HTTP 和 WebSockets,如 docs page 中所述。 AsyncConsumer 是基本的通用消费者 class,其他协议特定的消费者 class 就是从中派生出来的。其中 classes 之一是您提到的 AsynWebsocketConsumer。顾名思义,它用于与 websockets 一起工作,所以如果你想为你的实时应用程序使用 websockets,那么你应该使用 class。还有用于 HTTP 的 AsyncHttpConsumer。您很可能想使用 websockets,因此请选择 AsynWebsocketConsumer 或其派生词 AsyncJsonWebsocketConsumer

我还建议您阅读文档以更详细地了解受支持的协议以及如何以及何时使用它们。