I am currently working on a voice chat app as a personal project and this app will be like Discord, where users can hop into a chat room and start talking to people in that room. I am only going to make it voice only for now (I will implement text messaging later). I have been looking at examples on Github and I am having a little trouble with how to make the Consumer.
Here is what I have right now:
import json
from asgiref.sync import async_to_sync
from channels.generic.websocket import WebsocketConsumer
class VoiceConsumer(WebsocketConsumer):
def connect(self):
self.room_name = self.scope['url_route']['kwargs']['room_name']
self.room_group_name = "chat_%s" % self.room_name
# join the room
async_to_sync(self.channel_layer.group_add)(
self.room_group_name, self.channel_name
)
self.accept()
def disconnect(self, closed_code):
async_to_sync(self.channel_layer.group_discard)(
self.room_group_name, self.channel_name
)
def receive(self, text_data):
text_data_json = json.loads(text_data)
# Ask about this, how to receive voice ?
voice_message = text_data_json['message']
async_to_sync(self.channel_layer.group_send)(
self.room_group_name, {'type' : 'chat_message', 'message': voice_message}
)
I am currently working on a voice chat app as a personal project and this app will be like Discord, where users can hop into a chat room and start talking to people in that room. I am only going to make it voice only for now (I will implement text messaging later). I have been looking at examples on Github and I am having a little trouble with how to make the Consumer.
Here is what I have right now:
import json
from asgiref.sync import async_to_sync
from channels.generic.websocket import WebsocketConsumer
class VoiceConsumer(WebsocketConsumer):
def connect(self):
self.room_name = self.scope['url_route']['kwargs']['room_name']
self.room_group_name = "chat_%s" % self.room_name
# join the room
async_to_sync(self.channel_layer.group_add)(
self.room_group_name, self.channel_name
)
self.accept()
def disconnect(self, closed_code):
async_to_sync(self.channel_layer.group_discard)(
self.room_group_name, self.channel_name
)
def receive(self, text_data):
text_data_json = json.loads(text_data)
# Ask about this, how to receive voice ?
voice_message = text_data_json['message']
async_to_sync(self.channel_layer.group_send)(
self.room_group_name, {'type' : 'chat_message', 'message': voice_message}
)
As you see, I am confused about the text_data_json[‘message’] part. This is based off an example I have seen where someone implements messages, but I want to implement voice only. What do I change about this to where I can implement voice only ?
text_data_json = json.loads(text_data)
# Ask about this, how to receive voice ?
voice_message = text_data_json['message']
async_to_sync(self.channel_layer.group_send)(
self.room_group_name, {'type' : 'chat_message', 'message': voice_message}
)
I haven’t seen any examples that do this for voice only. I really appreciate the help.