minimal-webrtc/camera/consumers.py

43 lines
1.3 KiB
Python

from channels.generic.websocket import AsyncWebsocketConsumer
class VideoConsumer(AsyncWebsocketConsumer):
async def connect(self):
self.room_name = self.scope['url_route']['kwargs']['room_name']
self.kind = self.scope['url_route']['kwargs']['kind']
self.listen_group_name = '%s_%s' % (self.kind, self.room_name)
other_kind = 'client' if self.kind == 'host' else 'host'
self.send_group_name = '%s_%s' % (other_kind, self.room_name)
# Join room group
await self.channel_layer.group_add(
self.listen_group_name,
self.channel_name
)
await self.accept()
async def disconnect(self, close_code):
# Leave room group
await self.channel_layer.group_discard(
self.listen_group_name,
self.channel_name
)
# Receive message from WebSocket
async def receive(self, text_data):
await self.channel_layer.group_send(
self.send_group_name,
{
'type': 'chat_message',
'message': text_data
}
)
# Receive message from room group
async def chat_message(self, event):
message = event['message']
# Send message to WebSocket
await self.send(text_data=message)