channels文档中有Using Outside Of Consumers,介绍了如何在消费者类外使用channel_layer
from channels.layers import get_channel_layer
from asgiref.sync import async_to_sync
channel_layer = get_channel_layer()
async_to_sync(channel_layer.group_send)("chat", {
"type": "chat.force_disconnect"})
这里的type一度让我很迷茫,之前在consumers类中,理解的是该类中的一个自定义的函数,但是都out of consumers 怎么还用consumers类中的函数呢..
# 这里附上一般的consumer类
class ChatConsumer(WebsocketConsumer):
def connect(self):
self.room_group_name = "test"