Channels
Channels
Section titled “Channels”Django Channelsは、DjangoのWebSocketフレームワークです。
Channelsの設定
Section titled “Channelsの設定”依存関係の追加
Section titled “依存関係の追加”pip install channels channels-redisコンシューマーの実装
Section titled “コンシューマーの実装”from channels.generic.websocket import AsyncWebsocketConsumer
class ChatConsumer(AsyncWebsocketConsumer): async def connect(self): await self.channel_layer.group_add("chat", self.channel_name) await self.accept()
async def receive(self, text_data): await self.channel_layer.group_send( "chat", {"type": "chat_message", "message": text_data} )
async def chat_message(self, event): await self.send(text_data=event["message"])