Вебсокеты Flutter и подключение к каналам Django
возникают проблемы с переходом на соединение через websocket:
запускаю свой сервер на django (каналы) на
python manage.py runserver (my ip.. ):8000
и я продолжаю получать следующее:
WebSocket HANDSHAKING /ws/joingroup/0f248bf2-2e98-48a2-a8aa-96dece0bcff0/
WebSocket REJECT /ws/joingroup/0f248bf2-2e98-48a2-a8aa-96dece0bcff0/
WebSocket DISCONNECT /ws/joingroup/0f248bf2-2e98-48a2-a8aa-96dece0bcff0/
Во flutter я использую контроллер в getx.
WebSocketChannel? channel; //initialize a websocket channel
bool isWebsocketRunning = false;
void startStream() async {
if (isWebsocketRunning) return; //check if socket running
var url = 'ws://${ip}:8000/ws/joingroup/${Get.parameters['groupid']}/';
this.channel = WebSocketChannel.connect(
Uri.parse(url), //connect to a websocket
);
channel!.stream.listen(
(event) {
print(json.decode(event));
},
onDone: () {
isWebsocketRunning = false;
},
onError: (error) {
debugPrint('ws error $error');
}
);
файл my consumers.py в djagno:
class Chatconsumer(AsyncWebsocketConsumer):
async def connect(self):
print('hi')
self.group_id = self.scope['url_route']['kwargs']['groupid']
# gets the room_name from the Urlroute.
self.room_group_name = f'group {self.groupd_id}'
#this sets the room_group_name (basically creates a group)
await self.channel_layer.group_add(
self.room_group_name,
self.channel_name
)
await self.accept()
async def disconnect(self, close_code):
# Leave room group
await self.channel_layer.group_discard(
self.room_group_name,
self.channel_name
)
#consumer receives message from websocket
async def receive(self, text_data):
text_data_json = json.loads(text_data)
message = text_data_json['message']
await self.channel_layer.group_send(
self.room_group_name,
{
'type': 'chat_message',
'message': message
}
)
async def chat_message(self, event):
message = event['message']
await self.send(text_data=json.dumps({
'message': message
}))
Является ли это проблемой flutter и его запуска на виртуальной машине, где я не могу поддерживать соединение через вебсокет. Я использую слой inmemory channels. кто-то рекомендует попробовать запустить его с помощью ngrok?
любая помощь будет оценена по достоинству спасибо.